javascript - Chosen plugin: Change the background of options depending on its value -
for simple example's sake: i'd change background color of every option in dropdown list. every option should have background color corresponding value.
e.g.
$('...').css('background', 'red') i've tried select .active-result, won't work.
any ideas?
$(function() { var $select = $(document.getelementbyid('foo')), colors = ['red', 'yellow', 'green', 'purple', 'silver'] ; for(var = 0; < 5; i++) { $select[0].add(new option('color: ' + colors[i], colors[i])); } $select[0].options[2].selected = true; $select.chosen(); });
so, want set color of elements? try doing creating them , adding them <select>.
also, have jquery, use it! don't use mis-mash of jquery , native dom methods.
$(function(){ var $select = $('#foo'), // don't use getelementbyid colors = ['red', 'yellow', 'green', 'purple', 'silver']; for(var = 0, len = colors.length; < len; i++){ // don't hard-code length var $option = $('<option></option>', { // jquery can create elements text: 'color: ' + colors[i], value: colors[i] }).css('background-color', colors[i]); // set color $select.append($option); // append element using jquery } $select.val(colors[2]); // jquery can set "selected" option $select.chosen(); });
Comments
Post a Comment