-1

I need to add a new event listener to a dynamically created element and I cannot get the listener to attach.

I attempted this method with no success: add event listener on elements created dynamically

 b[0] = document.createElement("INPUT");
 b[0].name = "dt";
 b[0].type = "text";
 b[0].value = "YYYY-MM-DD HH:MM:SS";
 b[0].addEventListener('focus', removeVal);
 b[0].addEventListener('blur', addDTFormat);
 b[0].className = "fields";

Below is the screenshot from Chrome dev tools. I have the add listener method with the correct parameters, but in the properties pane the onblur and onfocus events are null for this element.

enter image description here

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
ExceptionLimeCat
  • 6,191
  • 6
  • 44
  • 77

1 Answers1

1

It should be focus and blur:

b[0].addEventListener('focus', removeVal);
b[0].addEventListener('blur', addDTFormat);

However IE's attachEvent requires on prefix.

dfsq
  • 191,768
  • 25
  • 236
  • 258
  • This isn't the issue. I changed the event type. The listener will not attach to the element. – ExceptionLimeCat Mar 20 '13 at 20:35
  • On what do you rely to say the event listeners did not attach to the element? Don't necessary trust what you see in debuggers. You should try to add the element to the DOM and see if your functions are getting called when you interact with the input. – plalx Mar 30 '13 at 04:09