Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add an onchange event to a select box via javascript?

I've got a script where I am dynamically creating select boxes. When those boxes are created, we want to set the onchange event for the new box to point to a function called toggleSelect().

I can't seem to get the syntax right to create the onchange event. Can someone tell me what I'm doing wrong? It doesn't throw an error, but doesn't work, either.

  col = dataRow.insertCell(0);   var transport_select = document.createElement('select');   transport_select.id = transport_select_id;   transport_select.options[0] = new Option('LTL', 'LTL');   transport_select.options[1] = new Option('FTL', 'FTL');   transport_select.onChange = function(){toggleSelect(transport_select_id);};   col.appendChild(transport_select); 
like image 868
user77413 Avatar asked Oct 27 '09 04:10

user77413


People also ask

How do I use Onchange in select tag in React?

To handle the onChange event on a select element in React: Set the onChange prop on the select element. Keep the value of the selected option in a state variable. Every time the user changes the selected option, update the state variable.

How do I use Onchange input tag?

Definition and UsageThe onchange attribute fires the moment when the value of the element is changed. Tip: This event is similar to the oninput event. The difference is that the oninput event occurs immediately after the value of an element has changed, while onchange occurs when the element loses focus.


1 Answers

Here's another way of attaching the event based on W3C DOM Level 2 Events Specification:

  transport_select.addEventListener(      'change',      function() { toggleSelect(this.id); },      false   ); 
like image 200
o.k.w Avatar answered Sep 27 '22 17:09

o.k.w