Select the specified option for alle html selects as jQuery.


/ Published in: jQuery
Save to your folder(s)

Select the specified option for alle html selects as jQuery.


Copy this code and paste it in your HTML
  1. /**
  2.  * Sets for all matched elements (DOM selected as jQuery) the option as selected.
  3.  *
  4.  * @param jQuery A jQuery object. Eg. $('select') or $(selector).
  5.  * @param option The optionvalue that will be selected. Eg. a number or string.
  6.  *
  7.  * @requires jQuery
  8.  * @author pogosheep < [email protected] >
  9.  */
  10. function setActiveSelectOption(jQuery, option) {
  11. $.each(jQuery.children(), function(index, element) {
  12. if ($(element).val() === option) {
  13. $(element).attr('selected', 'selected');
  14. }
  15. else {
  16. $(element).removeAttr('selected');
  17. }
  18. });
  19. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.