0

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>&nbsp;&nbsp;&nbsp;&nbsp;<div class='_25'><input type='textbox' id='textbox"+counter+"' name='stop"+counter+"' placeholder='Stop Name'></input></div>&nbsp;&nbsp;&nbsp;<div class='_25'><input type='textbox' id='textbox"+counter+"' name='timing"+counter+"' placeholder='Timing'></input></div>&nbsp;<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>
9
  • are you using plain javascript or jquery? Commented Apr 8, 2013 at 12:59
  • ya no jquery jus simple JS Commented Apr 8, 2013 at 13:00
  • Why don't you put everything inside a div and set it invisible. So when you click the button, just copy and paste HTML inside the div Commented Apr 8, 2013 at 13:01
  • When you say all the details vanish, do you mean that the data is cleared or the elements themselves disappear? Commented Apr 8, 2013 at 13:02
  • @guruprasath my data is getting cleared Commented Apr 8, 2013 at 13:04

2 Answers 2

1

you can try this way

var temp ="'<p>&nbsp;&nbsp;&nbsp;&nbsp;<div class='_25'><input type='textbox' id='textbox"+counter+"' name='stop"+counter+"' placeholder='Stop Name'></input></div>&nbsp;&nbsp;&nbsp;<div class='_25'><input type='textbox' id='textbox"+counter+"' name='timing"+counter+"' placeholder='Timing'></input></div>&nbsp;<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

Sign up to request clarification or add additional context in comments.

8 Comments

add this logic in 'generateRow()' function and dont forget to ++ your counter
i hv posted something this code is workiing fine on frnt end but as i am posting the form only 1 details ie of the very last input fields are getting posted but i want the value filled in all the created boxes to get posted
check name and ids of your elements, are they same? or different? is your counter logic works correctly?
although i hv incremented the counter ant the end of function
Shabbir now its woking i had just made a mistake ... i was just increasing the id with the counter as i was doing with name :that was my mistake::)
|
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.