0

I'm trying to load specific products into divs but each div is only loading in the first product. I'm guessing I need to do a loop of some sort but I'm not quite sure how

Here's what I'm trying to do:

<div class="prdContainer" rel="prod-1"></div>
<div class="prdContainer" rel="prod-2"></div>
<div class="prdContainer" rel="prod-3"></div>

$(document).ready(function() {
    var prdNo = $('.prdContainer').attr('rel').split('-')[1];

    $('.prdContainer').load('http://www.domain.com .product:nth-child(' + prdNo + ')');
});

In my mind this is what it SHOULD be doing:

$('.prdContainer').load('http://www.domain.com .product:nth-child(1)');
$('.prdContainer').load('http://www.domain.com .product:nth-child(2)');
$('.prdContainer').load('http://www.domain.com .product:nth-child(3)');

Which would be associated with:

rel="prod-1"
rel="prod-2"
rel="prod-3"

Any help would be great!

2 Answers 2

3
$(document).ready(function() {  
  $('.prdContainer').each(function(i){
    $(this).load('http://www.domain.com .product:nth-child(' + (i + 1)+ ')');
  });
});

you should know that index starts from 0

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks!! This works great. I was looking at it the wrong way it seems :)
@meskobalazs I tried to be faster but sometimes I can't do that .. My best Wishes to you :)
0

Use the attribute equals selector (official docs):

$('.prdContainer[rel="prod-' + prdNo + '"]').load(...);

Comments

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.