0

I have just started learning java script.I am trying to make Dynamic text box but the my code is not working . Can someone help me in rectifying my mistake??

 <html>
 <head>
 </head>
 <body>
 <script language="javascript">

     function add(){
         var text=document.createElement('input');
         var add=document.getElementById("Myform");
         text.setAttribute("type","text");
         text.setAttribute("name","BookName");
         add.appendChild(text);
     }
 </script>
 <form action="new1.php" id="Myform"> 
 <input type="submit" value="Submit" id="submit" onclick="javascript:add();"/>      <br/>

</form>
</body>
</html>

The text box disappears before I can do anything with it.

3 Answers 3

1

Change the input type as button or other wise it will submit the form.You want the submit button means you have to add one more button for dynamic action.

<input type="button" value="Submit" id="submit" onclick="javascript:add();"/> 
Sign up to request clarification or add additional context in comments.

Comments

0

You need stop submit like this:

 function add(){
 var text=document.createElement('input');
 var add=document.getElementById("Myform");
 text.setAttribute("type","text");
 text.setAttribute("name","BookName");
 add.appendChild(text);
 }
 <form action="new1.php" id="Myform" onsubmit="event.preventDefault();"> 
 <input type="submit" value="Submit" id="submit" onclick="javascript:add();"/>      <br/>

</form>

Comments

0

Try this plunker

<!DOCTYPE html>
<html>

  <head>
    <link rel="stylesheet" href="style.css">
    <script src="script.js"></script>
  </head>

  <body>
    <form action="new1.php" id="Myform"> 
 <input type="button" value="Submit" id="submit" onclick="add();"/>      <br/>

</form>
  </body>

</html>

JS

function add(){
 var text=document.createElement('input');
 var add=document.getElementById("Myform");
 text.setAttribute("type","text");
 text.setAttribute("name","BookName");
 add.appendChild(text);
 }

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.