I have two elements with the same ID on one page. When I attempt to append the data in each element into the corresponding div, it appends the data from the first element into both divs.
Here's my markup:
<div id="data" class="democlass" data-author="Mr. Jhon Doe" data-cat="Technology" data-url="http://www.youtube.com/"></div>
<div class="post">
<p class="data-author"></p>
<p class="data-cat"></p>
<a href="#" class="data-url"></a>
</div>
<div id="data" class="democlass" data-author="Mrs. Mona" data-cat="Personal" data-url="http://www.google.com/"></div>
<div class="post">
<p class="data-author"></p>
<p class="data-cat"></p>
<a href="#" class="data-url"></a>
</div>
Here's the jQuery I am using:
$(document).ready(function() {
var $ele = $("#data");
$( ".data-author" ).append($ele.attr("data-author"));
$( ".data-cat" ).append($ele.attr("data-cat"));
$( ".data-url" ).append($ele.attr("data-url"));
$(".data-url").attr('href' , 'http://example.com/test/' + $ele.data('url'))
});
How can append the first element's data to the first .post
, and the second element's data to the second .post
?
A working codepen for test