Question

I made an extract of my code to help explain this: http://jsfiddle.net/DF2Uw/1/

I basically want to see when a user changes the value of slottime. I use a simple onchange handler: slottime.setAttribute(onchange, alert("oh no"));

However this seems to trigger as soon as the select gets generated on the page instead of when the value is changed. I don't understand why, this makes no sense to me.

Can anybody explain the logic to me and maybe propose a fix?

Was it helpful?

Solution

  1. Use .addEventListener instead of setting attribute.
  2. You are not actually setting an attribute value. You are invoking a function alert(). Change it to: setAttribute('onchange', 'alert("oh no")')

Fiddle with .addEventListener: http://jsfiddle.net/DF2Uw/2/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top