Solved

adding table row with jquery & .clone and update input name and id's

Posted on 2009-05-12
4
10,680 Views
Last Modified: 2012-06-27
I am using .clone in jquery to add a row to an exisiting table with jquery. That works fine but I need to increment the number after the input name and ID in each row.

So based on the CODE example, If I added a row to the bottom it would be something like:

<tr>
      <td bgcolor="#ecf6fc" align="center">3</td>
      <td bgcolor="#ecf6fc"><input name="item_no3" type="text" id="item_no3" value="" /></td>
      <td bgcolor="#ecf6fc"><input name="item-desc3" type="text" id="item-desc3" value="" /></td>
</tr>

A forth row added would be

<tr>
      <td bgcolor="#ecf6fc" align="center">4</td>
      <td bgcolor="#ecf6fc"><input name="item_no4" type="text" id="item_no4" value="" /></td>
      <td bgcolor="#ecf6fc"><input name="item-desc4" type="text" id="item-desc4" value="" /></td>
</tr>

And so forth. I know I can grab the row count of the table and use append but Im not sure how Id do this with clone which is less code and more efficient to use for this purpose.
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
  $("#addrow").click(function() {
    $('#ORDERLINES tbody>tr:last').clone(true).insertAfter('#ORDERLINES tbody>tr:last');
    return false;
  });
});
</script>
 
<p><a id="addrow" href="#"><strong>Add Row</strong></a></p>
<table id="ORDERLINES" border="0" align="left" cellpadding="2" cellspacing="1" bgcolor="#999999">
  <thead>
    <tr>
      <th bgcolor="#ecf6fc" scope="col">No.</th>
      <th bgcolor="#ecf6fc" scope="col">Item Number</th>
      <th bgcolor="#ecf6fc" scope="col">Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td bgcolor="#ecf6fc" align="center">1</td>
      <td bgcolor="#ecf6fc"><input name="item_no1" type="text" id="item_no1" value="" /></td>
      <td bgcolor="#ecf6fc"><input name="item-desc1" type="text" id="item-desc1" value="" /></td>
    </tr>
    <tr>
      <td bgcolor="#ecf6fc" align="center">2</td>
      <td bgcolor="#ecf6fc"><input name="item_no2" type="text" id="item_no2" value="" /></td>
      <td bgcolor="#ecf6fc"><input name="item-desc2" type="text" id="item-desc2" value="" /></td>
    </tr>
  </tbody>
</table>

Open in new window

0
Comment
Question by:binovpd
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 12

Expert Comment

by:alien109
ID: 24368865
try this:

$(document).ready(function() {
  $("#addrow").click(function() {
    var row = $('#ORDERLINES tbody>tr:last').clone(true).insertAfter('#ORDERLINES tbody>tr:last');
    $("td:eq(0)", row).text($("#ORDERLINES tbody>tr").length);
    return false;
  });
});
0
 

Author Comment

by:binovpd
ID: 24370098
Thanks Alien09. This uses the count to increment the number in the first row which works great, but I am also trying to update the input name and id.  Something like

$(document).ready(function() {
  $("#addrows").click(function() {
    var row = $('#ORDERLINES tbody>tr:last').clone(true).insertAfter('#ORDERLINES tbody>tr:last');
    var rowcnt = $("#ORDERLINES tbody>tr").length
    var prevcount = (cnt - 1);
    $("td:eq(0)", row).html('<strong>' + $("#ORDERLINES tbody>tr").length + '.</strong>');
    $("input:eq(0)", row).('[@name=item-no' + prevcount + ']').attr("id", "item-no"+cnt );
    $("input:eq(0)", row).('[@name=item-no' + prevcount + ']').attr("id", "item-no"+cnt );
    return false;
  });
});

With you example if I add a  3rd row its updated like this.

<tr>
      <td bgcolor="#ecf6fc" align="center">3</td>
      <td bgcolor="#ecf6fc"><input name="item_no2" type="text" id="item_no2" value="" /></td>
      <td bgcolor="#ecf6fc"><input name="item-desc2" type="text" id="item-desc2" value="" /></td>
</tr>

I'd need it updated like this
<tr>
      <td bgcolor="#ecf6fc" align="center">3</td>
      <td bgcolor="#ecf6fc"><input name="item_no3" type="text" id="item_no3" value="" /></td>
      <td bgcolor="#ecf6fc"><input name="item-desc3" type="text" id="item-desc3" value="" /></td>
</tr>


0
 
LVL 12

Accepted Solution

by:
alien109 earned 125 total points
ID: 24370139
sorry - i missed that part. easy enough:

$(document).ready(function() {
  $("#addrow").click(function() {
    var row = $('#ORDERLINES tbody>tr:last').clone(true).insertAfter('#ORDERLINES tbody>tr:last');
    var index = $("#ORDERLINES tbody>tr").length;
    $("td:eq(0)", row).text(index);
    $("td:eq(1) input", row).attr("name", "item_no" + index).attr("id", "item_no" + index)
    $("td:eq(2) input", row).attr("name", "item-desc" + index).attr("id", "item-desc" + index)

    return false;
  });
});
0
 

Author Closing Comment

by:binovpd
ID: 31580784
Bingo!  Thats it.  Jquery really makes this simple once you wrap your head around it.  Thanks alien09
0

Featured Post

Learn by Doing. Anytime. Anywhere.

Do you like to learn by doing?
Our labs and exercises give you the chance to do just that: Learn by performing actions on real environments.

Hands-on, scenario-based labs give you experience on real environments provided by us so you don't have to worry about breaking anything.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
Introduction If you're like most people, you have occasionally made a typographical error when you're entering information into an online form.  And to your consternation, the browser remembers the error, and offers to autocomplete your future entr…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question