Skip to content

JQuery: Manipulate Select Button

here some jquery tricks for select button:

anyway, i just use following tricks coulpe minutes ago 😀

here we go!

/* sort options by text */

 

jQuery(“select#color”).html(jQuery(‘select#color option’).sort(function(x, y) {
return jQuery(x).text() < jQuery(y).text() ? -1 : 1;
}))
jQuery(“select#color”).get(0).selectedIndex = 0;

 

/* sort options by value */

 

jQuery(“select#color”).html(jQuery(‘select#color option’).sort(function(x, y) {
return jQuery(x).val() < jQuery(y).val() ? -1 : 1;
}))
jQuery(“select#color”).get(0).selectedIndex = 0;

/* remove option has empty value */

jQuery(“select#color option[value=”]”).remove();

 

/* add option to top of list */

 

jQuery(“select#color”).prepend(“<option value=” selected=’selected’>Color</option>”);

 

/* add option to bottom of list */

 

jQuery(“select#color”).append(“<option value=” selected=’selected’>Color</option>”);

Share

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.