views:

844

answers:

3

Hi,

How to iterate through table if it has this structure:

<table>
  <tr>
    <td><input class = "fire"> ... </td>
    <td><input class = "water"> ... </td>
  </tr>
  <tr>
    <td><input class = "fire"> ... </td>
    <td><input class = "water"> ... </td>
  </tr>
  <tr>
    <td><input class = "fire"> ... </td>
    <td><input class = "water"> ... </td>
  </tr>
</table>

I need at each iteration do this:

iterating:
     $("fire").val(newValue1); 
     $("water").val(newValue2);
+2  A: 

Query for the class names:

 $(".fire").val(newValue1); 
 $(".water").val(newValue2);

Or:

$("table input.fire").each(function (i) {
  $(this).val("input " + i);
});

$("table input.water").each(function (i) {
  $(this).val("input " + i);
});
Ionuț G. Stan
+2  A: 

Why do you need to iterate?

$('tr').each( function(){
    $(this).find('input.fire').val(newValue1);
    $(this).find('input.water').val(newValue2);
});
cpharmston
because i need always set different values on each <tr> where is fire and water classes
faya
Edited answer, then.
cpharmston
+1  A: 

If you need each iteration to have its own value, you could do this:

$("tr").each(function(i) {
    var newValue1 = "Some value"+i;
    var newValue2 = "Some other value"+i;
    $(this).find(".fire").val(newValue1);
    $(this).find(".water").val(newValue2);
});
peirix