Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding an event handler to an HTML select.option

I want to show a warning message if a user selects a specific option, but the warning isn't appearing. How can I modify my code so that this works correctly? Here is a demo on jsFiddle which reproduces the problem?

HTML :

<input type="text" id="mail_address"/>
<select>
    <option value='google.com'>google.com</option>
    <option onClick="warningaa()" value=''>Don't send mail</option>
</select>

JS:

function warningaa() {
    alert('If you choose this option, you can not receive any information');
}
like image 947
goJson Avatar asked Sep 15 '14 12:09

goJson


People also ask

How can you assign event handler to some HTML element?

The most flexible way to set an event handler on an element is to use the EventTarget. addEventListener method. This approach allows multiple listeners to be assigned to an element, and for listeners to be removed if needed (using EventTarget. removeEventListener ).

Which method is used to attach an event handler function to an HTML element?

The mouseup() method attaches an event handler function to an HTML element.

How do you event bind an event handler?

bind() method is used for attaching an event handler directly to elements. Handlers are attached to the currently selected elements in the jQuery object, so those elements must exist at the point the call to . bind() occurs. For more flexible event binding, see the discussion of event delegation in .

When user makes a selection in the HTML select list?

The <select> element is used to create a drop-down list. The <select> element is most often used in a form, to collect user input. The name attribute is needed to reference the form data after the form is submitted (if you omit the name attribute, no data from the drop-down list will be submitted).


3 Answers

You can not use on click action in dropdown option. One solution is to use change on select element:

html

<input type="text" id="mail_address" />
<select onchange="warningaa(this);">
    <option value='google.com'>google.com</option>
    <option value='error'>error</option>
</select>

js

function warningaa(obj) {
    if(obj.value == "error") {
        alert('If you choose this option, you can not receive any infomation');
    }
}

fiddle

like image 147
Alex Char Avatar answered Oct 16 '22 02:10

Alex Char


The option tag does not support the onclick event. Use the onchange event on the select instead.

HTML

<input type="text" id="mail_address"/>
<select id="selectbox" onchange="warning(this)">
  <option value='google.com'>google.com</option>
  <option value='warning'>Do not send me any kind of shit</option>
</select>

JS

function warning(obj) {
  if(obj.value == 'warning') {
    alert('If you choose this option, you can not receive any infomation');
  }
}
like image 29
Jesse Buitenhuis Avatar answered Oct 16 '22 01:10

Jesse Buitenhuis


You need to set an event handler on the SELECT element, and watch the "value" of select, as below:

document.getElementById('mySelect').addEventListener('change', warn, true);
function warn(e) {
  e.preventDefault();
  e.stopPropagation();
  if (e.currentTarget.value === 'the value you want') {
    // do something
  } else {
  return;
}

The key here is using CHANGE event vs CLICK, since you want to react to a "change in value" and if that value = something, warn the user. using addEventListener is also a better approach overall, it clearly distinguishes your HTML from your JavaScript.

More on this here:

https://developer.mozilla.org/en-US/docs/Web/API/EventTarget.addEventListener

and here:

https://developer.mozilla.org/en/docs/Web/API/Event

like image 37
Sebastien Daniel Avatar answered Oct 16 '22 01:10

Sebastien Daniel