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
anyone any ideas how to do that?
The <span> tag is an inline container used to mark up a part of a text, or a part of a document.
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.
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.
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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With