Ad

Adding Onclick Event To Html Select Option

- 1 answer

I added onclick event to my select tag like in the following code -

<option onclick="alert('Hello')">Say Hello</option>

but it does not work....

How can I set the optiononclick event to fire ?

Ad

Answer

Try it in the onchange handler:

function checkAlert(evt) {
  if (evt.target.value === "Say Hello") {
    alert('Hello');
  }
}
<select onchange="checkAlert(event)">
  <option>Test</option>
  <option>Say Hello</option>
</select>

Ad
source: stackoverflow.com
Ad