views:

54

answers:

2

I have a question about jQuery. Let's say I have a dropdown list:

 <select id="Positions" name="PositionType"> 
        <option value="Manager">Manager</option> 
        <option value="Janitor">Janitor</option> 
        <option value="Cook">Cook</option> 
        <option value="Cashier">Cashier</option> 
        <option value="Other">Other</option> 
  </select>

How could I display an alert box when "other" is selected? So far this does not work:

  <script type="text/javascript"> 
      $(document).ready(function(){
          $("#position").change(function(){
              if($("#position option:selected").val() == "other"){
                  alert("hello world");
              }
              else { 

              }
          });
      }); 
  </script> 
A: 

In your script, try "Other" with capital-O.

Y. Shoham
Or put `.toLowerCase()` after the `.val()`.
jerone
+2  A: 

Two things. First, as others have mentioned, "other" needs to be uppercase as string comparison is case sensitive. Second, you could retrieve the value of a select much easier:

  <script type="text/javascript"> 
      $(document).ready(function(){
          $("#position").change(function(){
              if($(this).val() == "Other"){
                  alert("hello world");
              }
              else { 

              }
          });
      }); 
  </script> 

A select element's value is equal to the value of it's selected option :-)

Mike Sherov
Thanks, I didn't realize the case sensitive part. I replaced my script with yours, but it's still not working :-( Any more suggestions? Here's my test site in case the HTML is wrong: http://www.box.net/shared/a0v69odcvu
Steven
the box link you sent is broken, but I think the problem might be that in the jquery, you have the id as #position, and in the HTML, you have it as "Positions"
Mike Sherov
I ended up deleting the file and reuploading it so here's a working link: http://www.box.net/shared/fekilcq1i9
Steven
Yes, that worked. This issue has resolved. Thanks for your help!
Steven
yup, that was it, change "#position" to "#Positions" to match the HTML
Mike Sherov