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 7 of 7
  1. #1
    By@
    By@ is offline
    New Coder
    Join Date
    Sep 2005
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up Create new fields via onClick

    Im trying to create a new field when the user presses a button.

    The thing is, I dont want the page to reload and lose data previously entered into other fields.

    Also, I want the name of the field to have a number which increments by 1 each time this button is pressed.

    For example,
    The button is there and when its click, a field called field1 is created. If the button is clicked again, another field is created called field2. and so on.


    Thanks in advance

  • #2
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Do a google for createElement(), should get you started...
    http://www.w3schools.com/htmldom/dom_obj_document.asp

    Good luck

  • #3
    By@
    By@ is offline
    New Coder
    Join Date
    Sep 2005
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Awesome. I think I got it now...

    Thanks champ

  • #4
    By@
    By@ is offline
    New Coder
    Join Date
    Sep 2005
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    function addField(){
           newField = document.createElement("text");
           newField.value= "This is the new field";
           document.myForm.appendChild(newField);
    }
    Code:
     
    <form name="myForm\">
    <input type="button" value=\"Add new field" onClick="addField();">
    </form>
    I cant seem to get it to create the new field... What am I doing wrong?

  • #5
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Theres no element called "text", I believe you wanted "input".
    newField = document.createElement("INPUT");
    Also, the slashe here:
    <form name="myForm\">
    Needs to be removed.

    Good luck;

  • #6
    By@
    By@ is offline
    New Coder
    Join Date
    Sep 2005
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Awesome! It works Yay!

    Thankyou so much.

    Yeah the slash was there because Im using it in amongst php.

  • #7
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    You're welcome


  •  

    Posting Permissions

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