0
votes

This question is a follow up of post Failed to set event handler in javascript

I want to add an event handler for input text controls. The input box control is generated dynamically. My code is like:

_inputbox = document.createElement("input");
_inputbox.type = "text";
_inputbox.id = settings[zindex];
_inputbox.onblur = checkName; 

checkName() is defined previously. But when I input something in the box and move the focus to other control, the checkName() isn't executed.

In the DOM tab of Firebug, I find the onblur is assigned to checkName() correctly.

In the HTML tab of Firebug, I find the input box only defines an "ID" and a "type". No "onblur" in its HTML code. If I edit the HTML and add onblur=checkName() manually. The function can be called successfully.

HTML code

<input type="text" id="Datastore">

Is there anyone can help me? Thanks a lot.

1
Your code works for me: jsfiddle.net/PvLew. Is checkName in the scope where you assign it?Felix Kling
@Felix Thank you. Actually the input box will be append to a cell as a part of table. I'm trying to isolate the code to find where the root reason is.Landy
Are you sure your page is not cached and you are looking at old code that did not add an event handler?epascarello
Can you post the actual code, as Felix says, as posted it looks okJaime

1 Answers

0
votes

Try

If(_inputbox.addEventListener) {
    _inputbox.addEventListener('blur', checkName, false);
} else { 
    _inputbox.attachEvent('onblur', checkName);
}