1

I'm using HTML 5 data attributes to key rows of data in a table. I need to iterate the rows and gather the data into an object collection. I created a class to represent my row data:

function SomeItem(description, finalPrice, percentDiscount) {
    this.Description = description;
    this.FinalPrice = finalPrice;
    this.PercentDiscount = percentDiscount;
}

An event fires which triggers the collection of this data.

$.each($('.ItemPriceBox'), function () {
            var uniqueid = $(this).data('uniqueid');
            var finalPrice = $(this).val();
        });

The final piece of data, percentDiscount should be retrieved using the data-uniqueid attribute. I'm not sure how to do this.

I want...

SomeItem[] items;
$.each($('.ItemPriceBox'), function () {
            var uniqueid = $(this).data('uniqueid');
            var finalPrice = $(this).val();
            var percentDiscount = $('.ItemDiscountBox').where("uniqueid = " + uniqueid);
            items[i] = new SomeItem(uniqueid,finalPrice,percentDiscount);
        });

How can I solve this?

2 Answers 2

3

may be like this

var percentDiscount = $(".ItemDiscountBox[uniqueid='"+uniqueid +"']");
Sign up to request clarification or add additional context in comments.

1 Comment

+1 Thats pretty close. I just finished up with a slightly different solution.
0

Instead of your pseudo-code where, use filter:

var percentDiscount = $('.ItemDiscountBox').filter("[uniqueid='" + uniqueid + "']");

$('.ItemPriceBox').each( function (i) {
    var uniqueid = $(this).data('uniqueid');
    var finalPrice = $(this).val();
    var percentDiscount = $('.ItemDiscountBox').filter("[uniqueid='" + uniqueid + "']");
    items[i] = new SomeItem(uniqueid,finalPrice,percentDiscount);
});

You can also use .each() instead of jQuery.each()

1 Comment

This is the right idea, but 3nigma's solution is more concise.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.