Garth Marenghi Garth Marenghi - 5 months ago 16
jQuery Question

Selecting every first option of multiple select elements

I have an html file with multiple select elements. What is needed is that the text of every first option of a select element is changed with jQuery. The answer given is

. But this does not change all the first option texts, only the first one.

What is the proper way to go about this?


Use .each():

$('select').each(function() {

Note that you should use text(), not html() in this case, as the content of <option> elements is supposed to be plain text.

Alternatively, you can select the first option elements directly, avoiding the use of children() and first():

$('select>option:first-child').each(function() {