user991945 user991945 - 1 year ago 61
jQuery Question

assign multiple element attributes with .each?

So I have the following table, and I'd like to set all the name attributes to a new value when I add a row to the table. (You probably guessed it: I use the buttons to add and delete rows, and I calculate a new name attribute based on the row index).

<table id="orderstable" >
<td align="top"><img src="/images/images/add_button_sm.gif" id="add_" align="top" border="0">
<img src="/images/images/delete_button_sm.gif" id="del_" border="0">
<td><input name="orderBoxes[0].A" size="3" value="0.0" type="text"></td>
<td><input name="orderBoxes[0].B" size="3" value="0.0" type="text"></td>
<td><input name="orderBoxes[0].C" size="3" value="0.0" type="text"></td>
<td><select name="orderBoxes[0].D">
<option value="fifty">50</option>
<option value="oneHundred" selected="selected">100</option>
<option value="twoHundred">200</option>
<option value="threeHundred">300</option></select>

I've tried to use .each to get all the elements and then get at the attribbute.... but I can't get it to work. Any thoughts? My code may not be the most efficient... I'm still a noob:

$("#orderstable > tbody > tr > td > img[id=add_]").click( function(event){
var row = $(this).closest("tr").get(0);
var rowCopy=$(row).clone(true);

$('#orderstable tbody>tr:last>td').each(function(){
}).val = "test";

Answer Source

This will grab the last row, and all input form elements. Then you can just set the name via $(this).attr('name')

$('#orderstable tr:last td :input').each(function(){
   $(this).attr('name', 'setyournamehere')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download