fuerteVentura22 fuerteVentura22 - 4 months ago 13
jQuery Question

Add text to all frist option

In my Html I have:

<select>
<option value></option>
<option value="1">One</option>
...
</select>
<select>
<option value></option>
<option value="1">One</option>
...
</select>
<select>
<option value></option>
<option value="1">One</option>
...
</select>
...


(All select tag without id or class)
I want to set the text of the first options

Es:

<select>
<option value>My Text</option>
<option value="1">One</option>
</select>
<select>
<option value>My Text</option>
<option value="1">One</option>
</select>
...


How can I do with jQuery?

Answer

Get first option using :first-child pseudo-class selector and update text content using text() method.

$('select option:first-child').text('My text')

$('select option:first-child').text('My text')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select>
  <option value></option>
  <option value="1">One</option>
</select>

<select>
  <option value></option>
  <option value="1">One</option>
</select>

<select>
  <option value></option>
  <option value="1">One</option>
</select>