tags:

views:

37

answers:

4

How would I check to see if the first option is selected and then execute some code.

Something like, if select box named "My_Saved_Shipping" has the 1st option selected then execute the code.

<select name="My_Saved_Shipping">
<option>Select/Enter Address</option>
<option value="1">text</option>
<option value="2">text2</option>
</select>
+1  A: 

Edit: Modified selector to work off of name, also added code to wireup to the change handler for the select, and placed in the ready handler.

$(function(){ 
        $("select[name='My_Saved_Shipping']").change(function() {            
            if (this.selectedIndex == 0){
                 <!-- do stuff -->
                }
          });
}); 
kekekela
Again No access to select markup, unfortunately
@user357034 updated, also included the jquery to wire up to the change event for the select
kekekela
+2  A: 
if ( $('select[name=My_Saved_Shipping]')[0].selectedIndex === 0 ) {
    // do something
}

It'd be best to give the <select> an id (typically matching the name) - an #id selector is faster than a [attr=val] selector.

J-P
+1 For not using jQuery ;p
DavidYell
In his example, it's however the 2nd option (which has an index of 1). I think the question is best to be reinterpreted as "Execute code when first option **with a value** is selected" ;)
BalusC
@BalusC, how do we know which `<option>` the OP is referring to?
J-P
The one which makes sense.
BalusC
For whatever reason this is not working on the page even though it should. How would we check selected text "Select/Enter Address" instead of the index = 0?
@user357034: If you want to test the current value then try: `if( $('select[name=My_Saved_Shipping]').val() === 'some string' ) ...`
J-P
To get the 'Select/Enter Address' part, use `.text == 'Select/Enter Address'`
Brendan Bullen
@user How are you invoking this script, can you verify that its even being called? Maybe you could add what you've got so far to the OP. I'm wondering if maybe you haven't added the code to wire up your javascript to the onchange/onclick/whatever handler for the element. (since no-one has provided that and you don't have access to the markup which would just allow you to add it there)...edited my answer to better show what I'm talking about
kekekela
A: 

Why not use somthing like:

<select name="My_Saved_Shipping">
  <option value="-1">Select/Enter Address</option>
  <option value="1">text</option>
  <option value="2">text2</option>
</select>

and test if value is negative ?

RC
No access to select markup, unfortunately
+1  A: 
<script type="text/javascript">
    if($("select[name='My_Saved_Shipping']").selectedIndex == 0)
    {
        //This is where your code goes
        alert('First Element is selected');
    }
</script>

If you want to test for the text within the option, use:

<script type="text/javascript">
    if($("select[name='My_Saved_Shipping']").text == 'Select/Enter Address')
    {
        //This is where your code goes
        alert('First Element is selected');
    }
</script>
Brendan Bullen