Welcome to WebmasterWorld Guest from 54.205.75.60

Forum Moderators: open

Message Too Old, No Replies

Traversing table rows method - why one works when other does not.

   
7:23 pm on May 30, 2012 (gmt 0)

5+ Year Member



Hi all,

while i do have a solution for traversing table rows in order to calculate unitprice, quantity and cost...

I was wondering why my first attempt does not work while the other does.


this does not work using: $(this+' input[name="a field"]')
function recalcTotals(){
var subtotal = 0.00;

// get each item line cost and calculate subtotal.
$('#workorder-form #sa-table-1 tbody tr').each(function (){

if($(this+' input[name="wi-Cost"]').val() != ''){

$(this+' input[name="wi-Cost"]').val(parseFloat($(this+' input[name="wi-UnitPrice"]').val()) * parseFloat($(this+' input[name="wi-QTY"]').val()));

subtotal += parseFloat($(this+' input[name="wi-Cost"]').val());
}
});


this does work using: $(this).find('input[name="a field"]')
function recalcTotals(){
var subtotal = 0.00;

// get each item line cost and calculate subtotal.
$('#workorder-form #sa-table-1 tbody tr').each(function (){

if($(this).find('input[name="wi-Cost"]').val() != ''){

$(this).find('input[name="wi-Cost"]').val(parseFloat($(this).find('input[name="wi-UnitPrice"]').val()) * parseFloat($(this).find('input[name="wi-QTY"]').val()));

subtotal += parseFloat($(this).find('input[name="wi-Cost"]').val());
}
});


It seems to me that both selector methods reference the same element.

thanks.
8:55 pm on May 30, 2012 (gmt 0)

WebmasterWorld Senior Member fotiman is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month



The jQuery function ($) takes a string selector or an object as it's parameter.

$(this+' input[name="a field"]')

That will coax this object to a string by implicitly calling it's toString() method, which might give you something like:
[Object object]
rather than the element you were hoping for.
9:23 pm on May 30, 2012 (gmt 0)

5+ Year Member



[Object object] is exactly what i got.

I'll leave it alone since the method that works appears to be the proper method in this case.