24

I need to run a jquery function if a certain dropdown option is selected.

<select name=dropdown size=1>
    <option value=1>option 1</option>
    <option value=2>option 2</option>
</select>

I have the commands in the function ready, Im just not sure on how to make it so if option 2 is currently active in the dropdown, then run the function.

(dropdown doesnt have a submit button, i want it ran when the user highlights the option)

0

7 Answers 7

42

Try this demo http://jsfiddle.net/JGp9e/

This will help, have a nice one!

code

$('select[name="dropdown"]').change(function(){

    if ($(this).val() == "2"){
        alert("call the do something function on option 2");
     }        
});​

HTML

<select name="dropdown" size=1>
    <option value="1">option 1</option>
    <option value="2">option 2</option>
</select>​
Sign up to request clarification or add additional context in comments.

1 Comment

what if you want it triggered even if the same thing was selected ?
7

use

$("#idofselectbox").change(function(){
// your code here
});

hope this helps....

Comments

7

Try this one, Demo on JsFiddle

$('select[name="dropdown"]').change(function() {
    alert($(this).val());

});

Comments

4
$(document).ready(function() {
  $("select[name='dropdown']").change(function() {
     alert($(this).val());
  });
});

Comments

2

You need to use change function

$('select[name=dropdown]').change(function() {
    alert($(this).val());
});

Comments

0
    $(document).ready(function() {
      $("select[name='dropdown']").change(function() {
         if($(this).val()==2){
             //do what u want here
           }//u can check your desired value here
     });
   });

I think you want this.

Comments

0

A simple way can be as following:

<select name="select" id="select" onChange="window.location = this.value">
  <option value="/en">English</option>
  <option value="/ps">Pashto</option>
</select>

Comments

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.