I'm currently working on a project where I need to separate the three table rows within a dynamically generated table. My goal is to have jQuery treat each row individually in order to extract specific values like price and SKU. You can see an example of what I'm trying to achieve in the fiddle link provided below.
Since all the rows have the same classes for each product, my approach involves defining each tr as a unique variant. From there, I can search through them using .text() to retrieve the desired values.
Here's a snippet of the code (jsfiddle link included):
jQuery:
var firstprice = $('#checkout .orderitems table.wizrtable tbody tr td.priceeach').text().replace(/\u00A3/g, '');
// Price of a single product in checkout
var amountofproducts = $('#checkout .wizrtable tbody tr').length;
// Number of table rows in the checkout structure
$(document).ready(function () {
if (+amountofproducts >= 4 && +amountofproducts <= 8) {
// Define unit price
var firstrow = $('.wizrtable').closest('table').children('tr:first');
var firstprice = $(firstrow.find('#checkout .orderitems table.wizrtable tbody tr td.priceeach')).text().replace(/\u00A3/g, '');
var secondrow = $('.wizrtable').closest('table').children('tr:eq(1)');
var secondprice = $(secondrow.find('#checkout .orderitems table.wizrtable tbody tr td.priceeach')).text().replace(/\u00A3/g, '');
var thirdrow = $('.wizrtable').closest('table').children('tr:eq(2)');
var thirdprice = $(thirdrow.find('#checkout .orderitems table.wizrtable tbody tr td.priceeach')).text().replace(/\u00A3/g, '');
// Define item IDs for each product
var firstSKU = $(firstrow.find('#checkout .orderitems table.wizrtable tbody .name .sku')).text().replace("Product Code: ", "");
var secondSKU = $(secondrow.find('#checkout .orderitems table.wizrtable tbody .name .sku')).text().replace("Product Code: ", "");
var thirdSKU = $(thirdrow.find('#checkout .orderitems table.wizrtable tbody .name .sku')).text().replace("Product Code: ", "");
// Inject html div with result
var Firstprodprice = jQuery("FirstprodPrice").text(firstprice);
}
});
JS Fiddle Link: http://jsfiddle.net/hslincoln/7HXBQ/10/
Any advice or suggestions?