Creating a HTML DOM select element in Javascript and having an onchange event

dom, html, javascript, onchange, select

Solution

The event name should be lowercase, and you need to assign a function instead of a string.

sel.onchange = someJavascriptFunction;

If there's more work to do, you can assign an anonymous function.

              //  v----assign this function
sel.onchange = function() {
    // do some work
    someJavascriptFunction();  // invoke this function
};

Problem

I am creating HTML DOM elements using Javascript (yes, I know it's nasty). I can get the select element created but I cannot add an onchange property. Example: ``` var sel = document.createElement('select'); sel.id = 'someID'; sel.title = 'Some title'; sel.style.fontWeight = 'bold'; sel.size = 1; ``` Once I have added the options and have done a `document.getElementById('someOtherID').appendChild(sel);` I get my select element. What I want to do is, at A above, add: `sel.onChange = 'someJavascriptFunction()';` but it doesn't like it. Should I be thinking outside of the square? Which one?

Original source