Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to span a color block along with text in options tag

Tags:

Is it not just as easy as this?

<select>
<option>Red<span style="width:7px;height:7px;background:red"></span></option>
<option>Green<span style="width:7px;height:7px;background:green"></span> 
</option>
</select>

trying to acheive output like below

colorDropdown

anyone any ideas how to do that?

like image 603
Sashi Avatar asked Jun 20 '18 08:06

Sashi


People also ask

Can I use span for text?

The <span> tag is an inline container used to mark up a part of a text, or a part of a document.

Can I put span inside option?

So depending on context there are two things that you can put inside an <option> — text or nothing at all — you may not put a <span> or any other element there. An option element cannot have any child elements.

Can you put a span in an a tag?

SPAN is a GENERIC inline container. It does not matter whether an a is inside span or span is inside a as both are inline elements.


1 Answers

You can't use other html tags or css styling on select options. But you can use 3rd libraries to render dropdowns and use it like a select. For example Bootstrap

$(function() {
   $('#my-select').find('li').click(function() {
      $('#selected').html($(this).html());
   });
});
<head>
      <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="stylesheet">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.3/jquery.min.js" type="text/javascript"></script>
    <script class="cssdeck" src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.3.1/js/bootstrap.min.js"></script>
</head>
<body>
<!-- Single button -->
<div id='my-select' class="btn-group">
  <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
    <span id="selected">Colors</span> <span class="caret"></span>
  </button>
  <ul class="dropdown-menu">
    <li><a href="#"><span style='width:10px; height:10px; background-color:red;display:inline-block;'></span> Red</a></li>
    <li><a href="#"><span style='width:10px; height:10px; background-color:blue;display:inline-block;'></span> Blue</a></li>
    <li><a href="#"><span style='width:10px; height:10px; background-color:green;display:inline-block;'></span> Green</a></li>
  </ul>
</div>
</body>
like image 195
Alberto Avatar answered Sep 28 '22 17:09

Alberto