Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery each - combining (this) with class specification

I'm trying to cycle through some table rows. The simplified rows are as follows:

<table>
  <tr id="ucf48">
    <td class="ucf_text">
      <input name="ucf_t48" value="Ann becomes very involved in the text she is reading." type="text">
    </td>
  </tr>
  <tr id="ucf351">
    <td class="ucf_text">
      <input name="ucf_t351" value="Ann is a fast and confident reader." type="text">
    </td>
  </tr>
</table>

I'm using this code to cycle:

$('#ucf tr').each(function(i,obj){
    var cn=$(this).attr('id').substr(3);
    var t=$(this +'.ucf_text input').val();

    console.log("Row "+i);
    console.log("Cnum: "+cn);
    console.log(t);
});

The console output is:

Row 0
Cnum: 48
Ann becomes very involved in the text she is reading.
Row 1
Cnum: 351
Ann becomes very involved in the text she is reading.

Now before someone flames me, I know I can do this another way by referring to the data I want using 'name'. Why, however, does my cnum variable follow 'this' but the t variable does not?

Thanks.

like image 492
Nick Avatar asked Nov 30 '22 15:11

Nick


2 Answers

When you do:

var t=$(this +'.ucf_text input').val();

this isn't converting correctly to a string.

Try:

var t=$(this).find('.ucf_text input').val();
like image 147
iambriansreed Avatar answered Dec 10 '22 21:12

iambriansreed


var t=$(this +'.ucf_text input').val();

You're trying to concatenate a string with a DOM node. I assume you want the children of each row? Which would be:
var t=$(this).find('.ucf_text input').val();

like image 22
Sam Dufel Avatar answered Dec 10 '22 21:12

Sam Dufel