Welcome to WebmasterWorld Guest from 23.20.157.174

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)

Full Member

5+ Year Member

joined:June 30, 2008
posts: 318
votes: 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.
8:55 pm on May 30, 2012 (gmt 0)

Senior Member from US 

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

joined:Oct 17, 2005
posts: 4996
votes: 14


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)

Full Member

5+ Year Member

joined:June 30, 2008
posts: 318
votes: 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.
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members