I have the following JavaScript code:
var ans_el = document.createElement( 'input' ); ans_el.setAttribute( 'id', unique_int_value ); ans_el.setAttribute( 'type', 'radio' ); ans_el.setAttribute( 'name', 'group' ); ans_el.setAttribute( 'value', 'myValue' ); ans_el.onclick = myFunction( this.id, this.value );
This, of course, does not work properly. At least not in Firefox 3.6. It happens that the onclick event is fired when the element is created, and the arguments passed to myFunction are zero. After adding an item to the DOM, the onclick event does not fire when a radio button is selected.
I would be grateful if someone finds out what is happening here, and / or how to add event handlers dynamically.
javascript javascript-events
Bruce
source share