G
getsanjay.sharma
Hello to all Javascript programmers out there.
I have been trying to dynamically create form elements based on a
button click.
It seems as though IE has problem with setting the properties of
elements once it has been created. Also considering that "input" form
element is an empty tag i.e. doesn't have an ending tag, the
document.createElement("input") creates an element of the form
<input ...> without an ending "/". Creating a form element dynamically
in a cross browser compatible manner seems to be a herculean task.
Here is my failed attempt at it:
<html>
<head>
<script type="text/javascript">
COUNTER = 0;
function createText(parentId)
{
var frmElement = document.getElementById(parentId);
var nameId = "txt" + COUNTER++;
if(document.all) //IE
{
var elem = document.createElement("<input type = 'text' name='" +
nameId +"' id='" + nameId + "' \/>");
}
else
{
var elem = document.createElement("input");
elem.type = "text";
elem.id = elem.name = nameId;
}
var br = document.createElement("br");
frmElement.appendChild(elem);
alert(document.getElementById('body').innerHTML); //dump the
generated html
elem.appendChild(br);
}
</script>
<body id="body">
<form action="#" id="frm">
<input type="button" value="Create" id="btn" name="btn" onmouseover =
"this.style.fontSize = '120px'; " onclick="createText('frm');" /><br /</form>
</body>
</html>
Any kind of snippet / links / other help on this topic would be highly
appreciated.
Thanks and regards,
STS
I have been trying to dynamically create form elements based on a
button click.
It seems as though IE has problem with setting the properties of
elements once it has been created. Also considering that "input" form
element is an empty tag i.e. doesn't have an ending tag, the
document.createElement("input") creates an element of the form
<input ...> without an ending "/". Creating a form element dynamically
in a cross browser compatible manner seems to be a herculean task.
Here is my failed attempt at it:
<html>
<head>
<script type="text/javascript">
COUNTER = 0;
function createText(parentId)
{
var frmElement = document.getElementById(parentId);
var nameId = "txt" + COUNTER++;
if(document.all) //IE
{
var elem = document.createElement("<input type = 'text' name='" +
nameId +"' id='" + nameId + "' \/>");
}
else
{
var elem = document.createElement("input");
elem.type = "text";
elem.id = elem.name = nameId;
}
var br = document.createElement("br");
frmElement.appendChild(elem);
alert(document.getElementById('body').innerHTML); //dump the
generated html
elem.appendChild(br);
}
</script>
<body id="body">
<form action="#" id="frm">
<input type="button" value="Create" id="btn" name="btn" onmouseover =
"this.style.fontSize = '120px'; " onclick="createText('frm');" /><br /</form>
</body>
</html>
Any kind of snippet / links / other help on this topic would be highly
appreciated.
Thanks and regards,
STS