Phorden
Phorden

Reputation: 1034

Copy dateTime text from one element into another element

I would like to copy the dateTime text that is inside of the eventDate div into the dateNumber span element. With my current code, it will copy the first date it finds inside of eventDate and post it inside of every dateNumber span element, thus each date is repeated and not unique. I would like the unique content from each eventDate div to be reflected in the dateNumber span element.

I am using the formatted date time text both for sorting the parent divs and also for displaying the date in this format: "Aug 7th".

html:

 <div data-event-date="1407128400000" class="panel callout radius columns small-12 medium-6 large-4 August">
    <span class="dateNumber"></span>
    <span><a href="/community-calendar/city-council-meeting-august-2014">City Council Meeting</a></span>
    <div data-bc-date="format: MMM Do" class="hidden-for-small-up eventDate">Aug 7th</div>
</div>

js:

$(".eventDate").parent().appendTo($(".dateNumber"));

Upvotes: 0

Views: 62

Answers (2)

Dhaval Marthak
Dhaval Marthak

Reputation: 17366

Try .closest() to target the ancestor.

For a single panel:

$(".eventDate").closest(".panel").find("span.dateNumber") // added span for more accuracy
                        .text($(".eventDate").html‌​());

For multiple panel:

$(".eventDate").each(function(){
    $(this).closest(".panel").find("span.dateNumber").text($(this).html()); //added span for more accuracy
});

Upvotes: 1

Ehsan Sajjad
Ehsan Sajjad

Reputation: 62488

if you are trying to set the .eventDate div value to the corresponding .dateNumber corresponding div,you can do like this:

$(".eventDate").each(function(){

$(this).parent().find(".dateNumber").text($(this).text());

});

Upvotes: 1

Related Questions