I wanted to create a dropdown select which has images instead of text as the options. I've done some Googling and searching here on Stack Overflow, and the answer generally given is to use the jQuery combobox.
The problem with this solution, it seems to me, is that you have to provide text. It looks like the images are just icons that accompany that text on the left. Correct me if I'm wrong, but this solution wouldn't cover what I'm trying to do-- which is completely replace the text with images.
Some background on what I'm trying to do-- I'm trying to create a dropdown for users to select line thickness on an online painting/doodling app. The images would be lines of different thickness, kind of like mspaint.
This question is related to
javascript
jquery
html
css
I tried several jquery based custom select with images, but none worked in responsive layouts. Finally i came across Bootstrap-Select. After some modifications i was able to produce this code.
PLAIN JAVASCRIPT:
DEMO: http://codepen.io/tazotodua/pen/orhdp
var shownnn = "yes";_x000D_
var dropd = document.getElementById("image-dropdown");_x000D_
_x000D_
function showww() {_x000D_
dropd.style.height = "auto";_x000D_
dropd.style.overflow = "y-scroll";_x000D_
}_x000D_
_x000D_
function hideee() {_x000D_
dropd.style.height = "30px";_x000D_
dropd.style.overflow = "hidden";_x000D_
}_x000D_
//dropd.addEventListener('mouseover', showOrHide, false);_x000D_
//dropd.addEventListener('click',showOrHide , false);_x000D_
_x000D_
_x000D_
function myfuunc(imgParent) {_x000D_
hideee();_x000D_
var mainDIVV = document.getElementById("image-dropdown");_x000D_
imgParent.parentNode.removeChild(imgParent);_x000D_
mainDIVV.insertBefore(imgParent, mainDIVV.childNodes[0]);_x000D_
}
_x000D_
#image-dropdown {_x000D_
display: inline-block;_x000D_
border: 1px solid;_x000D_
}_x000D_
#image-dropdown {_x000D_
height: 30px;_x000D_
overflow: hidden;_x000D_
}_x000D_
/*#image-dropdown:hover {} */_x000D_
_x000D_
#image-dropdown .img_holder {_x000D_
cursor: pointer;_x000D_
}_x000D_
#image-dropdown img.flagimgs {_x000D_
height: 30px;_x000D_
}_x000D_
#image-dropdown span.iTEXT {_x000D_
position: relative;_x000D_
top: -8px;_x000D_
}
_x000D_
<!-- not tested in mobiles -->_x000D_
_x000D_
_x000D_
<div id="image-dropdown" onmouseleave="hideee();">_x000D_
<div class="img_holder" onclick="myfuunc(this);" onmouseover="showww();">_x000D_
<img class="flagimgs first" src="http://www.google.com/tv/images/socialyoutube.png" /> <span class="iTEXT">First</span>_x000D_
</div>_x000D_
<div class="img_holder" onclick="myfuunc(this);" onmouseover="showww();">_x000D_
<img class="flagimgs second" src="http://www.google.com/cloudprint/learn/images/icons/fiabee.png" /> <span class="iTEXT">Second</span>_x000D_
</div>_x000D_
<div class="img_holder" onclick="myfuunc(this);" onmouseover="showww();">_x000D_
<img class="flagimgs second" src="http://www.google.com/tv/images/lplay.png" /> <span class="iTEXT">Third</span>_x000D_
</div>_x000D_
<div class="img_holder" onclick="myfuunc(this);" onmouseover="showww();">_x000D_
<img class="flagimgs second" src="http://www.google.com/cloudprint/learn/images/icons/cloudprintlite.png" /> <span class="iTEXT">Fourth</span>_x000D_
</div>_x000D_
</div>
_x000D_
Use combobox and add the following css .ddTitleText{ display : none; }
No more text, just images.
This is using ms-Dropdown : https://github.com/marghoobsuleman/ms-Dropdown
But data resource is json.
Example : http://jsfiddle.net/tcibikci/w3rdhj4s/6
HTML
<div id="byjson"></div>
Script
<script>
var jsonData = [
{description:'Choos your payment gateway', value:'', text:'Payment Gateway'},
{image:'https://via.placeholder.com/50', description:'My life. My card...', value:'amex', text:'Amex'},
{image:'https://via.placeholder.com/50', description:'It pays to Discover...', value:'Discover', text:'Discover'},
{image:'https://via.placeholder.com/50', title:'For everything else...', description:'For everything else...', value:'Mastercard', text:'Mastercard'},
{image:'https://via.placeholder.com/50', description:'Sorry not available...', value:'cash', text:'Cash on devlivery', disabled:true},
{image:'https://via.placeholder.com/50', description:'All you need...', value:'Visa', text:'Visa'},
{image:'https://via.placeholder.com/50', description:'Pay and get paid...', value:'Paypal', text:'Paypal'}
];
$("#byjson").msDropDown({byJson:{data:jsonData, name:'payments2'}}).data("dd");
}
</script>
I am a little to late on this, but you can do this using a simple bootstrap drop down and then do your code on select change event in any language or framework. (This is just a very basic solution, for other people like me who are just starting out and looking for a solution for a small simple project.)
<div class="dropdown">
<button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
Select Image
<span class="caret"></span>
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
<li> <a style="background-image: url(../Content/Images/Backgrounds/background.png);height:100px;width:300px" class="img-thumbnail" href=""> </a></li>
<li role="separator" class="divider"></li>
<li> <a style="background-image: url(../Content/Images/Backgrounds/background.png);height:100px;width:300px" class="img-thumbnail" href=""> </a></li>
</ul>
</div>
If you think about it the concept behind a dropdown select it's pretty simple. For what you're trying to accomplish, a simple <ul>
will do.
<ul id="menu">
<li>
<a href="#"><img src="" alt=""/></a> <!-- Selected -->
<ul>
<li><a href="#"><img src="" alt=""/></a></li>
<li><a href="#"><img src="" alt=""/></a></li>
<li><a href="#"><img src="" alt=""/></a></li>
<li><a href="#"><img src="" alt=""/></a></li>
</ul>
</li>
</ul>
You style it with css and then some simple jQuery will do. I haven't tried this tho:
$('#menu ul li').click(function(){
var $a = $(this).find('a');
$(this).parents('#menu').children('li a').replaceWith($a).
});
Seems like a straightforward html menu would be simpler. Use html5 data attributes for values or whatever method you want to store them and css to handle images as backgrounds or put them in the html itself.
Edit: If you are forced to convert from an existing select that you can't get rid of, there are some good plugins as well to modify a select to html. Wijmo and Chosen are a couple that come to mind
I hope this got you intrigued so here it goes. First, the html structure:
<div id="image-dropdown">
<input type="radio" id="line1" name="line-style" value="1" checked="checked" />
<label for="line1"></label>
<input type="radio" id="line2" name="line-style" value="2" />
<label for="line2"></label>
...
</div>
Whaaat? Radio buttons? Correct. We'll style them to look like a dropdown list with images, because that's what you're after! The trick is in knowing that when labels are correctly linked to inputs (that "for" attribute and target element id), the input will implicitly become active; click on a label = click on a radio button. Here comes comes slightly abbreviated css with comments inline:
#image-dropdown {
/*style the "box" in its minimzed state*/
border:1px solid black; width:200px; height:50px; overflow:hidden;
/*animate the dropdown collapsing*/
transition: height 0.1s;
}
#image-dropdown:hover {
/*when expanded, the dropdown will get native means of scrolling*/
height:200px; overflow-y:scroll;
/*animate the dropdown expanding*/
transition: height 0.5s;
}
#image-dropdown input {
/*hide the nasty default radio buttons!*/
position:absolute;top:0;left:0;opacity:0;
}
#image-dropdown label {
/*style the labels to look like dropdown options*/
display:none; margin:2px; height:46px; opacity:0.2;
background:url("http://www.google.com/images/srpr/logo3w.png") 50% 50%;}
#image-dropdown:hover label{
/*this is how labels render in the "expanded" state.
we want to see only the selected radio button in the collapsed menu,
and all of them when expanded*/
display:block;
}
#image-dropdown input:checked + label {
/*tricky! labels immediately following a checked radio button
(with our markup they are semantically related) should be fully opaque
and visible even in the collapsed menu*/
opacity:1 !important; display:block;
}
Full example here: http://jsfiddle.net/NDCSR/1/
NB1: you'll probably need to use it with position:absolute inside a container with position:relative +high z-index.
NB2: when adding more backgrounds for individual line styles, consider having the selectors based on the "for" attribute of the label like so:
label[for=linestyle2] {background-image:url(...);}
Source: Stackoverflow.com