Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 431
user1022585 Avatar asked May 07 '12 11:05

user1022585


People also ask

How to get selected value in dropdown jQuery?

Use the jQuery: selected selector in combination with val () method to find the selected option value in a drop-down list.

How do I select a specific DropDownList using jQuery?

Syntax of jQuery Select Option$(“selector option: selected”); The jQuery select option is used to display selected content in the option tag. text syntax is below: var variableValue = $(“selector option: selected”).

How do I get the selected value of dropdown?

The value of the selected element can be found by using the value property on the selected element that defines the list. This property returns a string representing the value attribute of the <option> element in the list.


2 Answers

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>​
like image 95
Tats_innit Avatar answered Oct 11 '22 14:10

Tats_innit


use

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

hope this helps....

like image 7
Philemon philip Kunjumon Avatar answered Oct 11 '22 13:10

Philemon philip Kunjumon