I need to swap out the text of a class (timeago) with the title dynamically. there will be multiple divs containing this class it could be 1 or it could be 1000
here is an example:
<abbr data-datetime="2022-10-30T18:54:39Z" title="10/30/2022 11:54 AM">15 days ago</abbr>
i appreciate the help
$('.timeago').attr('title', $('.timeago').text());
replaces titles with text but i need to do the opposite and for all .timeago classes, when i apply :firstchild to test it is still grabbing all the .timeago's text that exist..
is there an easy way to make this work?
CodePudding user response:
// jQuery 1
$(".timeago").each(function(){$(this).text($(this).attr("title"))});
// jQuery 2
$(".timeago1").each(function(){this.textContent = this.title});
// Vanilla-JS
document.querySelectorAll(".timeago2").forEach(e => e.textContent = e.title);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<abbr data-datetime="2022-10-30T18:54:39Z" title="10/30/2022 11:54 AM">15 days ago</abbr>
<abbr data-datetime="2022-10-30T18:54:39Z" title="10/30/2022 11:54 AM">15 days ago</abbr>
<br>
<abbr data-datetime="2022-10-30T18:54:39Z" title="10/30/2022 11:54 AM">15 days ago</abbr>
<abbr data-datetime="2022-10-30T18:54:39Z" title="10/30/2022 11:54 AM">15 days ago</abbr>
<br>
<abbr data-datetime="2022-10-30T18:54:39Z" title="10/30/2022 11:54 AM">15 days ago</abbr>
<abbr data-datetime="2022-10-30T18:54:39Z" title="10/30/2022 11:54 AM">15 days ago</abbr>