Home > OS >  How to pass div titles to other divs?
How to pass div titles to other divs?

Time:12-09

I have a function that is copying the title from one div to another but its only grabbing the first title.

$('.timeago').each(function() {
        var text = $(this).attr("title");
        $('.newDate').text(text);
    });

I need it to grab the titles (ie the dates in the image posted below) from each div with class .timeago instead of using the first item title in all .newDate divs.

enter image description here

the html looks like this:

<div >
    <div >10/31/2022 5:42 AM</div>
    <abbr  data-datetime="2022-10-30T18:54:39Z" title="10/30/2022 11:54 AM">15 days ago</abbr>
</div>

obviously each one would have a different title that I need for each .newDate

*edit - i forgot to mention that the divs .newDate don't exist in the html until i use prepend to add them ie:

$('.postedon').prepend('<div ></div>');

but i am prepending them before calling the function of discussion

CodePudding user response:

$(".postedon").each(function() {
    const ago = $(this).find(".timeago");
    const date = $(this).find(".newDate");
    date.text(ago.attr("title"));
})
  • Related