Reputation: 11
$(document).ready(function() {
var idPost = $('.post-outer').attr('id');
$('.post-outer').attr('name', idPost);
});
When you run the JS above it only takes the first "ID" repeating in other "DIV". What is going on:
<div class="post-outer" id="001" name="001">teste1</div>
<div class="post-outer" id="002" name="001">teste2</div>
<div class="post-outer" id="003" name="001">teste3</div>
I wanted to happen this:
<div class="post-outer" id="001" name="001">teste1</div>
<div class="post-outer" id="002" name="002">teste2</div>
<div class="post-outer" id="003" name="003">teste3</div>
Upvotes: 0
Views: 56
Reputation: 1809
$('.post-outer .post-footer .post-footer-line.post-footer-line-2 .post-labels a').each(function(index, el) {
$(this).parents('.post-outer:eq(0)').attr('itemtype', $(this).attr('href') );
});
Upvotes: -1
Reputation: 780788
The .attr()
function can take a function as the argument. It calls the function repeatedly for each element (like .each()
does), and the returned value is the new attribute value.
$('.post-outer').attr('name', function() {
return this.id;
});
This is essentially equivalent to mevius's answer, but it doesn't call .attr()
repeatedly.
Upvotes: 2