i am using this code:
<html>
<head>
<title>Dynamic Form</title>
<script type="text/javascript">
var i = 6;
function CreateTextbox(){
createTextbox.innerHTML = createTextbox.innerHTML
+'<input type=text name="flow'+i+'"/>'
i++;
}
</script>
</head>
<body>
<form name="form" action="post">
<input type="button" name="what" value="clickHere" onClick="CreateTextbox()"/>
<div id="createTextbox"></div>
</form>
</body>
when i add a new textbox, the value that was entered in the previous textbox is deleted. how can i retain it?
Adding HTML element by concatenating the innerHTML is very slow and that is causing the values to be cleared, since the container (the div
createTextbox
) and all the childs are re-created on each innerHTML assignment.I suggest you to create the input elements programmatically with
document.createElement
and append the elements using to the container div usingappendChild
:Check the above code working here.
You're probably forcing the browser to recreate the element when you redefine its innerHTML property, which subsequently causes you to lose its value in the process.
Can this be done with a
combination?