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

  • Home
  • SEARCH
  • 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 404209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:17:14+00:00 2026-05-12T17:17:14+00:00

I have been trying to make something work with JQuery. I have the following

  • 0

I have been trying to make something work with JQuery. I have the following function:

<script type="text/javascript">
$(function(){
    // start a counter for new row IDs
    // by setting it to the number
    // of existing rows
    var newRowNum = 2;

    // bind a click event to the "Add" link
    $('#addnew').click(function(){
        // increment the counter
        newRowNum += 1;

        // get the entire "Add" row --
        // "this" refers to the clicked element
        // and "parent" moves the selection up
        // to the parent node in the DOM
        var addRow = $(this).parent().parent();

        // copy the entire row from the DOM
        // with "clone"
        var newRow = addRow.clone();

        // set the values of the inputs
        // in the "Add" row to empty strings
        //$('input', addRow).val('');
        //$('name', addRow).val('os' + newRowNum);

        // replace the HTML for the "Add" link 
        // with the new row number
        $('td:first-child', newRow).html('<input type="hidden" name="on' + newRowNum + '" value="Email Address ' + (newRowNum - 1) + '">Recipient');

        // insert a remove link in the last cell
        $('td:last-child', newRow).html('<a href="" class="remove">Remove<\/a>');

        // loop through the inputs in the new row
        // and update the ID and name attributes
        $('td:first-child.next-child.input', newRow).each(function(i){
            var newID = newRowNum;
            $(this).attr('id','os' + newID).attr('name','os' + newID);
        });

        //$('td:first-child.next', newRow).html('<input type="text" id="os' + newRowNum + '" name="os' + newRowNum + '" value="">');

        // insert the new row into the table
        // "before" the Add row
        addRow.before(newRow);

        // add the remove function to the new row
        $('a.remove', newRow).click(function(){
            $(this).parent().parent().remove();
            return false;               
        });

        // prevent the default click
        return false;
    });
});
</script>

In the html, I have this (part of a larger code block):

<tr>
  <td><input type="hidden" name="on2" value="Email Address 1"><a id="addnew" href="">Add</a></td><td><input name="os2" type="text" id="os2" value="" size="24" maxlength="60" /></td>
  <td>&nbsp;</td>
</tr>

What is supposed to be happening here is that when the Add link is clicked, the Add is changed to the word Recipient with a hidden form element with name=”on + newRowNum” (incremented by one for each new row added) and the same for id. This works just fine. However, the second part of this in the next , I need to update the name and id of the text input to match the newRowNum, in this case name=”os + newRowNum” etc. I am not able to make this happen. I am most likely not using the proper form to access this. I have tried all of the following:

$('td:first-child.next-child.input', newRow).each(function(i){
    var newID = newRowNum;
    $(this).attr('id','os' + newID).attr('name','os' + newID);
});

$('td:firstChild.nextSibling.input', newRow).each(function(i){
    var newID = newRowNum;
    $(this).attr('id','os' + newID).attr('name','os' + newID);
});

plus many other versions of the above. Can anyone help me out with this? Hope this is not too vague.

  • 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-12T17:17:15+00:00Added an answer on May 12, 2026 at 5:17 pm

    This selector $('td:first-child.next-child.input', newRow) is a little bit wrong. If all we need to do is get the inputs in the row we could do

    $('input:hidden', newRow).attr('id','on' + newRowNum ).attr('name','on' + newRowNum );
    $('input:text', newRow).attr('id','os' + newRowNum ).attr('name','os' + newRowNum );
    

    Here’s a Working Demo. if you want to see the code, add /edit to the URL.

    BTW – I thought that you meant something like this originally until I re-read your question 🙂

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

Sidebar

Ask A Question

Stats

  • Questions 285k
  • Answers 285k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The answer to your question might be to start over… May 13, 2026 at 4:49 pm
  • Editorial Team
    Editorial Team added an answer Are you sure you're not seeing the correct behavior? You're… May 13, 2026 at 4:49 pm
  • Editorial Team
    Editorial Team added an answer I am assuming you are using visual studio. In visual… May 13, 2026 at 4:49 pm

Related Questions

I have been trying to make a case for using Python at my work.
I am trying to use OBJECT to embed content from a remote server into
I've been trying to interpret the answers to similar questions but haven't been able
I'm trying to work out a way to display the contents of the version

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.