myForm.appendChild(domElement) doesn't work [message #662216] |
Tue, 29 March 2011 14:26  |
g-dawg Messages: 2 Registered: March 2011 |
Junior Member |
|
|
I have the following javascript in my app
function addDataField(frmObj, typeStr, nameStr, valueStr){
alert(document.getElementsByTagName("form").length + " forms exist");
var fld = document.createElement("input");
fld.type = typeStr;
fld.name = nameStr;
fld.value = valueStr;
alert("input field created"); // <----- This displays fine
document.getElementsByTagName("form")[0].appendChild(fld);
alert(document.getElementsByName(nameStr)[0].name + "\n" +
document.getElementsByName(nameStr)[0].value); // <----- This never displays in Eclipse debugger
}
This functionality works wonderfully in IE and FF - but not in the Eclipse debugger tab. My big issue is that I'm trying to debug the PHP code that follows this functionality and I NEED this to function in order for the PHP to get the proper POST data. This could be a configuration issue (probably is) but I haven't been able to figure out why all of my other javascript is working but this one function/method is not.
[Updated on: Tue, 29 March 2011 15:19] Report message to a moderator
|
|
|
| Re: myForm.appendChild(domElement) doesn't work [message #662281 is a reply to message #662216] |
Wed, 30 March 2011 01:01  |
Toshihiro Izumi Messages: 268 Registered: July 2009 |
Senior Member |
|
|
>This functionality works wonderfully in IE and FF
Really? 'appendChild'-'getElementsByName' sequence doesn't work well in IE. (not knowing IE9)
<html>
<body>
<form></form>
<script type="text/javascript">
var fld = document.createElement("input");
fld.name = "test";
document.getElementsByTagName("form")[0].appendChild(fld);
alert(document.getElementsByName("test").length);
</script>
</body>
</html>
I got 0 on IE. (IE8 on WindowsXP)
This is known issue and you can see the solution at http://msdn.microsoft.com/en-us/library/ms536389%28v=VS.85%2 9.aspx
<html>
<body>
<form></form>
<script type="text/javascript">
var fld1 = document.createElement("input");
fld1.name = "test1";
document.getElementsByTagName("form")[0].appendChild(fld1);
alert(document.getElementsByName("test1").length);
var fld2 = document.createElement("<input name='test2'>");
document.getElementsByTagName("form")[0].appendChild(fld2);
alert(document.getElementsByName("test2").length);
</script>
</body>
</html>
I got 0 and 1.
However, this is too bad since it violates DOM spec.(It causes "String contains an invalid character" exception on Firefox)
'getElementById' resolve it.
|
|
|
Powered by
FUDForum. Page generated in 0.01412 seconds