jquery loop through select options set selected

I recently needed to set a default option for a SELECT drop down list. Add and Remove Options in Select using jQuery. Join Stack Overflow to learn, share knowledge, and build your career. Please tell us why you want to mark the subject as inappropriate. Event binding on dynamically created elements? Select change event; This one is only for changing the checkbox state based on option selection. Each function and array: 7. I have a tag in PHP. var data = { id: 1, text: 'Barn owl' }; var newOption = new Option(data.text, data.id, false, false); $('#mySelect2').append(newOption).trigger('change'); What you do is use the “selected-selector” of jQuery to do it in a single line. So when we click on the button, our jQuery code will find the option from the select tag which contains “Option 2” and adds the attribute “selected”. ... Sets or returns the index of the selected