tags:

views:

42

answers:

2

I need to get the name of each input field within a row, and change it. I am having difficulty in finding the proper syntax to reference the name field. For example:

<tr id="row_0" class="dataRow">
    <input type="text" class="tabcell" name="_0-3" size="6" value=7.0 />
    <input type="text" class="tabcell" name="_0-7" size="6" value=7.6 />

I iterate over the rows and want to rename each td field:

var namePrefix = "AFS";
$('td:eq(0)', this).each(function(e) {
    $('td:eq(0) input[name]', this).replaceWith($(namePrefix  + 'td:eq(0) input', this).val()); 
.... etc ... 

But this does not work. The end result should look like this:

<tr id="row_0" class="dataRow">
    <input type="text" class="tabcell" name="AFS_0-3" size="6" value=7.0 />
    <input type="text" class="tabcell" name="AFS_0-7" size="6" value=7.6/>

Anyone know how I can reference the input field name and change it?

Thanks.
Vic

+1  A: 

use the .attr and .removeAttr methods to change the name.

var name = $('.selector').attr('name'); // accesses name attribute
$('.selector').attr('name', myName); // changes/adds name
$('.selector').removeAttr('name'); // removes name
Jason
+1  A: 

I would think this would work:

$('tr input').each(function(){
    $(this).attr('name', 'ASF' + $(this).attr('name'));
});
Pat