I am creating a plugin for the selected menu to replace the ugly default selections and be consistent across OSs.
Here's a demo (firefox and webkit only)
http://spacirdesigns.com/selectMenu/
It already works, but I'm having trouble assigning the "selected" attribute. The code works with any other attribute, but I cannot get it to work with the selected attribute.
It works:
select.find('option') .removeAttr('whatever') .eq(index).attr('whatever', 'hello');
It does not mean:
select.find('option') .removeAttr('selected') .eq(index).attr('selected', 'selected');
And here is the code so far:
(function($){ $.fn.selectMenu = function() { var select = this; select.hide(); var title = select.attr('title'); var arrow = 'img/arrow.png'; var items = ''; select .children('option') .each(function(){ var item = $(this).text(); if ($(this).val() != '') { $(this).attr('value', item); } items += '<li>' + item + '</li>' }); var menuHtml = '<ul class="selectMenu">' + '<img src="' + arrow + '" alt=""/>' + '<li>' + title + '</li>' + '<ul>' + items + '</ul>' + '</ul>'; select.after(menuHtml); var menu = $(this).next('ul'); var list = menu.find('ul'); menu .hover(function(){}, function(){ list.hide(); }) .children('li').hover(function(){ list.show(); }); menu.find('ul li').click(function(){ var index = $(this).index(); menu.children('li').text($(this).text()); select.find('option') .removeAttr('selected') .eq(index).attr('selected', 'selected'); list.hide(); }); }; })(jQuery);
elclanrs
source share