Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6218691
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:39:14+00:00 2026-05-24T07:39:14+00:00

I wrote a simple script that add extra form fields when needed by the

  • 0

I wrote a simple script that add extra form fields when needed by the user: the html part consist of a first section where I define a form field pattern and a second part, ready for the insertion of the pattern.

<div id="readroot" style="display: none">
    <input type="text" value="test" id="name" name="name">
</div>

<form method="post" action="index.php">

    <span id="writeroot"></span>

    <a href=# onclick="moreFields();">Add record</a>
    <input type="submit" value="Confirm" />

</form>

The javascript part would have to exactly replicate the “readroot” div just before the “writeroot” span. It replicates everything, indeed, except the “name”, “id”, value” of the input fields (or, maybe, I’m not able to handle them). I also tried putting an onclick to detect the field name after it’s been “placed”, but it doesn’t return the correct name!!!

<script type="text/javascript">
  var counter = 0;
  window.onload = moreFields();

  function moreFields() {
     counter++;
     var newFields = document.getElementById('readroot').cloneNode(true);
     newFields.id = 'baba';
     newFields.style.display = 'block';
     var newField = newFields.childNodes;
     for (var i=0;i<newField.length;i++) {
        var theName = newField[i].name;
        if (theName)
           newField[i].name = theName + counter;
           newField[i].onclick = alert(this.name);
    }
    var insertHere = document.getElementById('writeroot');
    inserted = insertHere.parentNode.insertBefore(newFields,insertHere);
    }
</script>

Thank you in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T07:39:14+00:00Added an answer on May 24, 2026 at 7:39 am

    Your are setting the click event handle the wrong way. You are calling alert immediately and in this context , this will refer to the window object.

    It should be:

    newField[i].onclick = function() {alert(this.name);};
    

    Seems to work fine for me: http://jsfiddle.net/2KSds/

    DOM

    The name of the input field is correctly changed and the id is there. If you add the element more that one time you also have to change the baba id and the name id to something unique.


    The question is whether you need the IDs at all. Do you have to uniquely identify the fields? Using classes might be sufficient. Something like:

    <div id="readroot" style="display: none">
        <input type="text" value="test" name="name" class="inputClass">
    </div>
    

    and

    var readroot = document.getElementById('readroot'),
        writeroot = document.getElementById('writeroot');
    
    function moreFields() {
         counter++;
         var newFields = readroot.cloneNode(true);
         newFields.removeAttribute('id');
         newFields.className += 'fieldContainer';
    
         var newFields = newFields.childNodes;
         for (var i = 0, l = newField.length; i < l; i++) {
            var theName = newField[i].name;
            if (theName) {
               newField[i].name = theName + counter;
            }
            newField[i].onclick = function() {alert(this.name);}
         }
         inserted = writeroot.parentNode.insertBefore(newFields,writeroot);
    }
    

    You can then control the visibility through setting CSS rules for .fieldContainer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on (what should be) a simple script that will add text to
I wrote a simple batch file as a PowerShell script, and I am getting
I wrote a simple tool to generate a DBUnit XML dataset using queries that
I needed some simple string encryption, so I wrote the following code (with a
I have a simple Google App Engine app, that I wrote using ordinary strings.
I wrote a simple PyGTK script to show some basic process information in a
I wrote a python script that rotates an image 90 degrees. I am including
I wrote a simple PHP shell script which parses files and outputs certain element.
I'm trying to write a simple script that clones an existing Mercurial kiln repository
I wish to write a simple git script that will run the following lines:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.