Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2009
    Posts
    49
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Create a new form input element wit an onClick event

    I would like to create a new form text input field each time the plus symbol is clicked, also each new fields Id and Name attributes should increment.
    Here is the code i've got so far, i'm not good at codding but trying to learn.
    Code:
    <html>
    <head>
    <title>JavaScript</title>
    
    
    <script type="text/javascript">
    function addInput(){
    	var i;
    	for (i=0; i<5; i++) {
      document.write("<input type='text' name='name_" + i + "' id='name_" + i + "' maxlength='50' /> <a href='#' onclick='addInput()'>+</a><br>\n");
    						}
    						return false;
      }
    </script>
    
    
    
    </head>
    <body>
    <br>
    <form id='myform'>
    <a href="#" onclick="addInput()">+</a>
    </form>
    </body>
    </html>

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    974
    Thanks
    19
    Thanked 212 Times in 210 Posts
    maybe something like this? ...

    <html>
    <head>
    <title>JavaScript</title>
    <script type="text/javascript">
    var j=0;
    function addInput(el){
    var newInput = document.createElement("input");
    newInput.id = "id" + ++j;
    newInput.name = "name" + j;
    newInput.value = j;
    el.form.appendChild(newInput);
    el.form.appendChild(document.createElement("br"));
    }
    </script>
    </head>
    <body>
    <br>
    <form id='myform'>
    <button type="button" onclick="addInput(this)">+</button><br>
    </form>
    </body>
    </html>
    Last edited by DaveyErwin; 06-22-2014 at 11:32 PM.

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Even better is to completely separate the HTML and JavaScript like this:

    Code:
    <html>
     <head>
     <title>JavaScript</title>
      </head>
     <body>
     <br>
     <form id='myform'>
     <button id="but" type="button">+</button><br>
     </form>
    
    <script type="text/javascript">
    (function() {
     var j=0;
     function addInput(frm){ 
     var newInput = document.createElement("input");
    newInput.id = "id" + ++j;
    newInput.name = "name" + j;
    newInput.value = j;
     frm.appendChild(newInput);
     frm.appendChild(document.createElement("br"));
     }
     document.getElementById('but').addEventListener('click', addInput, false );
    })();
     </script>
     </body>
     </html>
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #4
    New Coder
    Join Date
    Jun 2014
    Posts
    23
    Thanks
    0
    Thanked 2 Times in 2 Posts
    since you are new to coding this one will come straight to you
    [code]
    <html>
    <head>
    <title>JavaScript</title>
    <script type="text/javascript">
    var j=0;
    function addInput(){
    var newInput = document.createElement("input");
    newInput.id = "id" + ++j;
    newInput.name = "name" + j;
    newInput.value = j;
    document.getElementById("myform").appendChild(newInput);
    document.getElementById("myform").appendChild(document.createElement("br"));
    }
    </script>
    </head>
    <body>
    <br>
    <form id='myform'>
    <button type="button" onclick="addInput()">+</button><br>
    </form>
    </body>
    </html>
    [/code


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •