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);
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.
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.
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 );
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With