Harlan said:
I'm using Javascript to insert an HTML element like
<input type="text" name="mytext" id="mytext" value="" onblur="alert('x');">
In both IE7 and Firefox 3, the input field appears, and I can type into
it, but when I tab out of it to the next field, the alert only happens
in Firefox.
Is the problem that IE doesn't bind events for dynamically generated
elements? Is there a way make this happen?
*Dang Motzarella will not let me cross post! I hope my ISP sorts out
their news server soon!*
Here is a very stripped down example to get you started...
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"
http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1">
<meta http-equiv="content-language" content="en-us">
<title>template</title>
<script type="text/javascript">
function insertField(){
var f=document.getElementById('daform');
var tb=document.createElement('input');
tb.setAttribute('name','mytext');
tb.setAttribute('id','mytext');
if( tb.addEventListener ) {
tb.addEventListener('blur',someFunc,false); //Proper
} else if( tb.attachEvent ) {
tb.attachEvent('onblur', someFunc); //IE only
}
else {
tb.onblur=someFunc; //old standby but does not append listeners
}
f.appendChild(tb);
}
function someFunc(){
alert("Im all set!");
}
// attach event after page loads
if( window.addEventListener ) {
window.addEventListener('load',insertField,false); //legacy
} else if( document.addEventListener ) {
document.addEventListener('load',insertField,false); //proper
} else if( window.attachEvent ) {
window.attachEvent('onload', insertField); //IE only
}
</script>
</head>
<body>
<form id="daform">
<input type="text" name="sample" value="existing element">
</form>
</body>
</html>