51

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);
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
user77413
  • 30,205
  • 16
  • 46
  • 52

5 Answers5

65

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
  );
rogerdpack
  • 62,887
  • 36
  • 269
  • 388
o.k.w
  • 25,490
  • 6
  • 66
  • 63
41

Add

transport_select.setAttribute("onchange", function(){toggleSelect(transport_select_id);});

setAttribute

or try replacing onChange with onchange

rahul
  • 184,426
  • 49
  • 232
  • 263
14

replace:

transport_select.onChange = function(){toggleSelect(transport_select_id);};

with:

transport_select.onchange = function(){toggleSelect(transport_select_id);};

on'C'hange >> on'c'hange


You can use addEventListener too.

Community
  • 1
  • 1
IgniteCoders
  • 4,834
  • 3
  • 44
  • 62
  • More generally for getting option value or id: `select_element.onchange = function(){console.log(this.id + ": " + this.value);};` – Bruno L. Jun 05 '21 at 14:46
10
yourSelect.setAttribute( "onchange", "yourFunction()" );
Patrick S.
  • 109
  • 1
  • 3
  • 2
    Hello and welcome to SO! You need to describe your answer. Also, you need to answer the question itself. user77413 is asking 'How to add an onchange event to a select box via javascript?' and also asks in their post 'Can someone tell me what I'm doing wrong?'. You need to address these in your answer. – Maytha8 Oct 25 '20 at 12:36
  • 1
    This answer is short and effective. – Peter Apr 20 '21 at 11:11
  • This was the only answer that worked for me out of the above. – Alexander McNulty Aug 06 '21 at 07:08
  • This is even better with `yourSelect.setAttribute( "onchange", "yourFunction(this)" );` – Alexander McNulty Aug 06 '21 at 07:31
  • 2
    It was 2020, well past time to put binding events with HTML attributes to bed, along with table-based layouts and answers with no text. Use `addEventListener` to separate behavior from structure and allow for more than one event listener per event and element. – Heretic Monkey Mar 24 '22 at 15:58
1

If you are using prototype.js then you can do this:

transport_select.observe('change', function(){
  toggleSelect(transport_select_id)
})

This eliminate (as hope) the problem in cross-browsers