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 8341205
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T05:24:25+00:00 2026-06-09T05:24:25+00:00

I have a form/table that is dynamically altered via JS by adding/removing rows (form

  • 0

I have a form/table that is dynamically altered via JS by adding/removing rows (form inputs). I want to create a column that counts the number of rows, so I tried using,

$("#rowcount:last").replaceWith( "<td>" + newNum + "</td>" );

to change the counter columns last element to the new number, but the ‘:last’ selector doesn’t seem to work in this case. Here is my code for reference (sorry for the ugliness of my form. I tried to make it look as pretty as I could, but because of my limited experience, it still looks like it was slapped on the wrong side at birth):

<html>

<head>
<title></title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
<script src="jquery.maskedinput.js" type="text/javascript"></script>

<script type="text/javascript">
    $(document).ready(function() {
        $('#btnAdd').click(function() {
            var num     = $('.ccinput').length; // how many "duplicatable" input fields we currently have
            var newNum  = new Number(num + 1);      // the numeric ID of the new input field being added

            // create the new element via clone(), and manipulate it's ID using newNum value
            var newElem = $('#input' + num).clone().attr('id', 'input' + newNum);

            // insert the new element after the last "duplicatable" input field
            $('#input' + num).after(newElem);
            $("#rowcount").replaceWith( "<td>" + newNum + "</td>" );

            // enable the "remove" button
            $('#btnDel').attr('disabled','');

            $("*#date").mask("99/99/9999");

            // business rule: you can only add 20 names
            if (newNum == 20)
                $('#btnAdd').attr('disabled','disabled');
        });

        $('#btnDel').click(function() {
            var num = $('.ccinput').length; // how many "duplicatable" input fields we currently have
            $('#input' + num).remove();     // remove the last element

            // enable the "add" button
            $('#btnAdd').attr('disabled','');

            // if only one element remains, disable the "remove" button
            if (num-1 == 1)
                $('#btnDel').attr('disabled','disabled');
        });

        $("*#date").mask("99/99/9999");
    });
</script>
</head>

<body>

<form method="post" action="<?php echo htmlentities($PHP_SELF); ?>">
<table border="1" cellspacing="0">
<tr>
<th></th>
<th># (last four digits)</th>
<th>Amount</th>
<th>Approval</th>
<th>Date Received</th>
</tr>
<tbody>
    <tr id="input1" class="ccinput">
    <td id="rowcount"> 1 </td>
    <td> <input id="cnum" type="text" name="cc_num[]" maxlength="4" /> </td> 
    <td> <input id="camnt" type="int" name="cc_amnt[]" /> </td>
    <td> <input id="appr" type="text" name="cc_app[]" maxlength="10" /> </td> 
    <td> <input id="date" type="text" name="cc_date[]" /> </td>
    </tr>
</tbody>
</table>
<div>
    <input type="button" id="btnAdd" value="Add" />
    <input type="button" id="btnDel" value="Remove" />
</div>
<input type="submit" value="Submit" name="submit" />
</form>

</body>
</html>
  • 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-06-09T05:24:28+00:00Added an answer on June 9, 2026 at 5:24 am

    You can use the :last selector:

     var newElem = $('.ccinput:last').clone().attr('id', 'input' + newNum);
     $('.ccinput:last').after(newElem);
     $(".ccinput:last td:first").replaceWith( "<td>" + newNum + "</td>" );
    

    or instead of replaceWith() you can try:

     $(".ccinput:last td:first").html(newNum).removeAttr('id');
    

    DEMO

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

Sidebar

Related Questions

I have a table body that grabs it's rows (and form fields) via AJAX
I have an Ajax form that lets me dynamically remove rows from a table
I have a form with fields that are generated dynamically via a loop. It's
I have a standard HTML formatted table, that dynamically generates the content, via Zend
I have a form that sends info into a database table. I have it
I have a form with the fields inside table cells. On the last column
I want to populate a table form a MYSQL database. I have defined the
I have a form that can be manipulated on the client's browser via javascript
To dynamically fill DropDown controls on my HTML Form, I have written code that
I have a button that will create a new entry (row in a table)

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.