javascript - Jquery .load() only for first 6 elements, with data-value -


i have 7 html div's. how can load dynamically data first 6 div's using data-recentviewes load-url each element?

html:

    <div id="recentviewes" data-recentviewes="/123/"></div>     <div id="recentviewes" data-recentviewes="/456/"></div>     <div id="recentviewes" data-recentviewes="/789/"></div>     <div id="recentviewes" data-recentviewes="/321/"></div>     <div id="recentviewes" data-recentviewes="/654/"></div>     <div id="recentviewes" data-recentviewes="/987/"></div> <!-- 6 --> ---     <div id="recentviewes" data-recentviewes="/abc/"></div> <!-- dont load --> 

js (work first element):

window.addeventlistener('load', function () {     var recentviewes = $("#recentviewes").data('recentviewes');         $("#recentviewes").each(function(index, element) {         $("#recentviewes").load("http://example.com" + recentviewes);                             return index < 5;     }); });  

thanks.

you need remove (or amend) id attributes need unique within document context. why code works first element.

you should change them class attributes instead. can use :lt selector select elements require:

<div class="recentviewes" data-recentviewes="/123/"></div> <div class="recentviewes" data-recentviewes="/456/"></div> <div class="recentviewes" data-recentviewes="/789/"></div> <div class="recentviewes" data-recentviewes="/321/"></div> <div class="recentviewes" data-recentviewes="/654/"></div> <div class="recentviewes" data-recentviewes="/987/"></div> <!-- 6 -->  <div class="recentviewes" data-recentviewes="/abc/"></div> <!-- dont load --> 
window.addeventlistener('load', function () {     $(".recentviewes:lt(6)").each(function() {         $(this).load("http://example.com" + $(this).data('recentviewes'));     }); }); 

Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -