Change the name of the first option

I have the following code, which I do not have access to.

What I want to do is add text to the first option, which is now empty. Text such as "Choose Address"

<select name="My_Saved_Billing"
onChange="Choose_My_Saved_Billing(this.selectedIndex)" style="background-color:#EEEEEE">
<option></option>
<option value="1394">text</option>
</select>
+3
source share
2 answers
$('select[name=My_Saved_Billing] > option:first-child')
    .text('Select Address');

If you want to find the empty option (s), and not just the first one, use:

$('select[name=My_Saved_Billing] > option:empty')
// or
$('select[name=My_Saved_Billing] > option:empty:first')

To get an option with specific content, use:

$('select[name=My_Saved_Billing] > option:contains(texthere)')
+10
source
$("select[name=My_Saved_Billing] option:first").text("Select Address");

Demo: http://jsfiddle.net/VGhdX/

To answer your question from your comment (if I understood correctly):

how to automatically select the first option that mattered or the option that is easier code

You can do this using the Enable attribute selector :

$("select[name=My_Saved_Billing] option[value]:first").text("Foo");

Has Attribute , ( value="" ).

+7

Source: https://habr.com/ru/post/1759462/


All Articles