Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

edit the first option name

Tags:

jquery

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

What I want to do is add some text into the first option which is now empty. Text such as "Select 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>
like image 759
user357034 Avatar asked Aug 13 '10 04:08

user357034


2 Answers

$('select[name=My_Saved_Billing] > option:first-child')
    .text('Select Address');

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

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

To get the option with specific content, use:

$('select[name=My_Saved_Billing] > option:contains(texthere)')
like image 196
strager Avatar answered Sep 28 '22 00:09

strager


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

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

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

how would automatically select the first option that had a value or option text whichever is easier to code

You can do this using the Has Attribute Selector:

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

The Has Attribute selector will ignore present attributes which contain empty values (so value="" will not match).

like image 20
karim79 Avatar answered Sep 27 '22 22:09

karim79