Search code examples
javascriptonclickhtml-select

adding onclick event to html select option


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 option onclick event to fire ?


Solution

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