David Bélanger
David Bélanger

Reputation: 7438

Event name when select an option inside a select

I know the event name when we change something in a select is change (html: onchange), but I would like to know what is the event name when I select (click) a specific option.

Example :

<select>
    <option>Option 1</option>
</select>

When I click on Option 1, what event happen ? Only change (on the select itself and all option) or something else ?

Thanks.

Upvotes: 7

Views: 9958

Answers (4)

Nishu Tayal
Nishu Tayal

Reputation: 20830

event.type returns the nature of event. Refer the Demo

$('select').bind('change', function(event) {
    alert("Event :"+ event.type);   
});

You can use .on/.bind on the element.​

Upvotes: 1

thecodeparadox
thecodeparadox

Reputation: 87073

By default, in most browser happen change event when you change select and click event for option.

$('select').on('change', function(event) {
  console.log(event.type);   // event.type is to get event name
});

Upvotes: 7

Shadow2531
Shadow2531

Reputation: 12170

'change' on the SELECT and 'click' on the OPTION. Also, in Opera, 'input' might fire on the SELECT too if you add the listener with addEventListener() for example.

Upvotes: 2

JohnFx
JohnFx

Reputation: 34909

You can put it in the onclick event of the option you are interested in like so:

<select id="MySelect">
    <option id="optionA" onclick="alert('You clicked A')">a</option>
    <option id="optionB">b</option>  
</select>

Upvotes: 0

Related Questions