I want to update a price calculation for each row with jQuery every time the user updates the Quantity in that row.
There are many rows with different items. Each row has its own id. Each row has a Qty input field and a output field with the same classes.
This is the basic HTML relevant to the case:
<div id="item1">
<div><input class="itemQty" onchange="alterQty()"></div>
<div><span class="itemPrice"></span></div>
</div>
<div id="item2">
<div><input class="itemQty" onchange="alterQty()"></div>
<div><span class="itemPrice"></span></div>
</div>
and this is the jQuery function I have sofar:
function alterQty() {
var rowID = $('.itemQty.').parent().parent().attr('id');
var rowQty = '#' + rowID + ' .itemQty';
var rowPrice = '#' + rowID + ' .itemPrice';
iQty = $(rowQty).val();
var iRetail = "100"; //(For the sake of this question this doesn't matter yet)
var itemPriceCalc = iRetail * iQty;
$(rowPrice).text(itemPriceCalc);
};
With the above, what happens is that any Qty updates the price of the first row. And all subsequent rows are not affected. The "rowID" being returned seems to be always "item1"...
What am I missing to make this function work for each specific Qty and give the output only to the Price field in that specific row?
I can write the same function a hundred times using a new name each time but there must be a better way to do this...
$('.itemQty.').parent().parent().attr('id');will always be the first itemQty parent id (item1) - which, I guess, is why you're asking the question.id(id=item1) then determine how you can do the same without the id. Use relative DOM navigation, but you need to start with the element you're on.