Welcome to WebmasterWorld Guest from 54.197.183.230 register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts, Pubcon Platinum Sponsor 2014
 Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

# JavaScript and AJAX Forum

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

Msg#: 4459586 posted 7:23 pm on May 30, 2012 (gmt 0)

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.

Fotiman

Msg#: 4459586 posted 8:55 pm on May 30, 2012 (gmt 0)

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.

nelsonm

Msg#: 4459586 posted 9:23 pm on May 30, 2012 (gmt 0)

[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.

 Global Options: top home search open messages active posts  Tweet

 Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.