tags:

views:

24

answers:

2

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>
+4  A: 
$("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).

karim79
This one worked as well. Two correct answers, how do I choose? LOL
Problem is I do not know what could be in the text. Is there something like .text(!="")
@user357034 Try `option[text!= '']:first`
karim79
+1  A: 
$('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)')
strager
This worked great, just a side question is, how would automatically select the first option that had a value or option text whichever is easier to code.
@user357034, I would recommend storing the initial selector in a variable. That way, you get the same item regardless if the text changes.
strager
I do not understand you previous comment. Or perhaps I didn't fully explain myself. Right now there are two options the first which is originally empty and the second which has a value and text. If there is no options with either a value or text how would we change the first option to say something else instead of "select address" like "Enter Address" I assume we have to check to see if all options values are null or text=""
"If there is no options with either a value or text" You can test for this using `$('select[name=My_Saved_Billing] > option:not(:empty)').length === 0`. Or by checking value: `$('select[name=My_Saved_Billing] > option[value=]').length === 0`
strager