user1022585
user1022585

Reputation: 13641

jquery function on dropdown select

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)

Upvotes: 24

Views: 85318

Answers (7)

Shamshad Zaheer
Shamshad Zaheer

Reputation: 247

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>

Upvotes: 0

Tats_innit
Tats_innit

Reputation: 34107

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>​

Upvotes: 42

Saifuddin Sarker
Saifuddin Sarker

Reputation: 863

    $(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.

Upvotes: 0

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

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

Upvotes: 4

Adil
Adil

Reputation: 148110

Try this one, Demo on JsFiddle

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

});

Upvotes: 7

Philemon philip Kunjumon
Philemon philip Kunjumon

Reputation: 1422

use

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

hope this helps....

Upvotes: 7

antyrat
antyrat

Reputation: 27765

You need to use change function

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

Upvotes: 2

Related Questions