Reputation: 25
I am trying to create two text box and one select box on button click function, but as I am filling boxes created by one click and on another click all the filled details get vanished.
I want the detail as I fill to be able to post in database ?
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<script>
var counter=1;
function generateRow() {
var d=document.getElementById("div");
d.innerHTML+="<p> <div class='_25'><input type='textbox' id='textbox"+counter+"' name='stop"+counter+"' placeholder='Stop Name'></input></div> <div class='_25'><input type='textbox' id='textbox"+counter+"' name='timing"+counter+"' placeholder='Timing'></input></div> <div class='_25'><strong>Select Route</strong><select id='ampm"+counter+"'name='ampm"+counter+"'><option>a.m</option><option>p.m</option></select> </div>";
counter++;
}
</script>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<div id="div"></div>
<p><input type="button" value="Add" onclick="generateRow()"/></p>
<p>
</form>
</body>
</html>
Upvotes: 0
Views: 903
Reputation: 6023
First get d.innerHTML
into another variable and new html in another variable. Then concat both of them and save in innerhtml
However, you should be building with insertBefore
or appendChild
functionality instead of the simple innerHTML
.
Upvotes: 0
Reputation: 2538
you can try this way
var temp ="'<p> <div class='_25'><input type='textbox' id='textbox"+counter+"' name='stop"+counter+"' placeholder='Stop Name'></input></div> <div class='_25'><input type='textbox' id='textbox"+counter+"' name='timing"+counter+"' placeholder='Timing'></input></div> <div class='_25'><strong>Select Route</strong><select id='ampm"+counter+"'name='ampm"+counter+"'><option>a.m</option><option>p.m</option></select> </div>'";
var newdiv = document.createElement('div');
newdiv.innerHTML = temp;
var yourDiv = document.getElementById('div');
yourDiv.appendChild(newdiv);
hope it will work
Upvotes: 1