0

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> 

2 Answers 2

2

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 :-)

Sign up to request clarification or add additional context in comments.

5 Comments

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: box.net/shared/a0v69odcvu
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"
I ended up deleting the file and reuploading it so here's a working link: box.net/shared/fekilcq1i9
Yes, that worked. This issue has resolved. Thanks for your help!
yup, that was it, change "#position" to "#Positions" to match the HTML
0

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

1 Comment

Or put .toLowerCase() after the .val().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.