Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery event when select option

What's the event to bind for when a select form is selected?

I have something like this:

<select id='list'> <option value='1'>Option A</option> <option value='2'>Option B</option> <option value='3'>Option C</option> </select> 

When Option B is selected, I want some function to run.

So what do I bind,

$("#list").bind("?", function (){ // How do I check if it's option b that's selected here //blah blah }); 
like image 213
Mark Avatar asked Oct 08 '09 03:10

Mark


People also ask

What is the event for select dropdown?

The HTML Select DropDownList has been assigned a jQuery OnChange event handler. When an item is selected in the HTML Select DropDownList, the jQuery OnChange event handler is executed within which the Text and Value of the selected item is fetched and displayed in JavaScript alert message box.

How can check select option selected or not in jQuery?

$('#mySelectBox option'). each(function() { if ($(this). isChecked()) alert('this option is selected'); else alert('this is not'); });

What is the use of select jQuery?

jQuery select() MethodThe select event occurs when a text is selected (marked) in a text area or a text field. The select() method triggers the select event, or attaches a function to run when a select event occurs.

What is select function in JavaScript?

select() method selects all the text in a <textarea> element or in an <input> element that includes a text field.


2 Answers

This jQuery snippet will get you started:

$('#list').change(function() {     if ($(this).val() === '2') {         // Do something for option "b"     } }); 
like image 80
dcharles Avatar answered Sep 23 '22 23:09

dcharles


the event you are looking for is change. more info about that event is available in the jquery docs here: http://docs.jquery.com/Events/change#fn

like image 40
Darko Z Avatar answered Sep 24 '22 23:09

Darko Z