S
stellstarin
I have a html where fields are created and added dynamically on the
client side. I use the
AppendChild() call to create fields dynamically.
On submit i try to get the value for all the elements in the form,
including those that are added
dynamically. I use document.getElementsByName('Field Name')to achieve
the same.
But this function returns 'undefined' for all dynamically added
elements.
Any pointers on why this happens ?
The sample HTML code is added below,
<html>
<head><title>Checking multiple fields with same name</title>
</head>
<script>
//------------------------------------------------------------------------------
// function to add the hidden field dynamically
function addField (form, fieldType, fieldName, fieldValue) {
var input = document.createElement('INPUT');
if (document.all) { // what follows should work
// with NN6 but doesn't in M14
input.type = fieldType;
input.name = fieldName;
input.value = fieldValue;
}
else if (document.getElementById) { // so here is the
// NN6 workaround
input.setAttribute('type', fieldType);
input.setAttribute('name', fieldName);
input.setAttribute('value', fieldValue);
}
form.appendChild (input);
}
//------------------------------------------------------------------------------
function check()
{
addField (document.checkform, "text", "newfield1",100);
var e = document.getElementsByName('newfield1') ;
alert("e value is "+e.value) ;//output for this is ->
'undefined' while the script is
executed
return true ;
}
</script>
<body>
<form name="checkform" method="POST" action="action.php" >
<input value="416223698001884" onClick="" name="fldFcatReference"
align="center" type="checkbox">
<input value="1" onClick="" name="fldStateBit" align="center"
type="hidden">
<input value="2662236980014" onClick="" name="fldFcatReference"
align="center" type="checkbox">
<input value="266223693333" onClick="" name="fldFcatReference"
align="center" type="checkbox">
<input value="266223694444" onClick="" name="fldFcatReference"
align="center" type="checkbox">
<input value="1" onClick="" name="fldStateBit" align="center"
type="hidden">
<input value="1" onClick="" name="fldradio" align="center"
type="radio">
<input value="2" onClick="" name="fldradio" align="center"
type="radio">
<input value="3" onClick="" name="fldradio" align="center"
type="radio">
<input value="4" onClick="" name="fldradio" align="center"
type="radio">
<input value="Submit" name="submit" align="center" onClick="check()"
type="submit">
</form>
</body>
</html>
client side. I use the
AppendChild() call to create fields dynamically.
On submit i try to get the value for all the elements in the form,
including those that are added
dynamically. I use document.getElementsByName('Field Name')to achieve
the same.
But this function returns 'undefined' for all dynamically added
elements.
Any pointers on why this happens ?
The sample HTML code is added below,
<html>
<head><title>Checking multiple fields with same name</title>
</head>
<script>
//------------------------------------------------------------------------------
// function to add the hidden field dynamically
function addField (form, fieldType, fieldName, fieldValue) {
var input = document.createElement('INPUT');
if (document.all) { // what follows should work
// with NN6 but doesn't in M14
input.type = fieldType;
input.name = fieldName;
input.value = fieldValue;
}
else if (document.getElementById) { // so here is the
// NN6 workaround
input.setAttribute('type', fieldType);
input.setAttribute('name', fieldName);
input.setAttribute('value', fieldValue);
}
form.appendChild (input);
}
//------------------------------------------------------------------------------
function check()
{
addField (document.checkform, "text", "newfield1",100);
var e = document.getElementsByName('newfield1') ;
alert("e value is "+e.value) ;//output for this is ->
'undefined' while the script is
executed
return true ;
}
</script>
<body>
<form name="checkform" method="POST" action="action.php" >
<input value="416223698001884" onClick="" name="fldFcatReference"
align="center" type="checkbox">
<input value="1" onClick="" name="fldStateBit" align="center"
type="hidden">
<input value="2662236980014" onClick="" name="fldFcatReference"
align="center" type="checkbox">
<input value="266223693333" onClick="" name="fldFcatReference"
align="center" type="checkbox">
<input value="266223694444" onClick="" name="fldFcatReference"
align="center" type="checkbox">
<input value="1" onClick="" name="fldStateBit" align="center"
type="hidden">
<input value="1" onClick="" name="fldradio" align="center"
type="radio">
<input value="2" onClick="" name="fldradio" align="center"
type="radio">
<input value="3" onClick="" name="fldradio" align="center"
type="radio">
<input value="4" onClick="" name="fldradio" align="center"
type="radio">
<input value="Submit" name="submit" align="center" onClick="check()"
type="submit">
</form>
</body>
</html>