How can I get the value of two attributes and put it in another attribute?

Viewed 781

I have a multiple calendar table and I want to combine all attribute in another attribute. Is it possible or not? Just experimenting. Here's a sample code of the * HTML *

<div class="day_num" data-date="17" date="July 2019">17</div>

I want to put the data-date and date attribute to this div attribute:

<div full-date=""></div>

Is this possible? sorry for experimenting such thing.

* This is a sample calendar *

enter image description here

Every cell has the div mention above. I will include the HTML output

enter image description here

2 Answers

This will loop through day_num elements, combine its attribute to form the date & assign that date to the child div into the full-date attribute.

$('.day_num').each(function(){
    let full_date = $(this).attr('data-date')+' '+$(this).attr('date');
    $(this).children('div').attr('full-date',full_date);
})

The attribute values are plain strings, which can be combined (concatenated) together as you want, using the + operator. I assume you would want to put "17 July 2019" as the full-date attribute (assuming here your elements have and ID tags "source" and "target", to make sure the selector returns a single element):

let day = $("#source").attr("data-date");
let date = $("#source").attr("date");
let fullDate = day + " " + date;
$("#target").attr("full-date", fullDate);

Edit: With the full HTML and table in the question; You'll need to loop over the elements $(".day_num"), and apply the same piece of code to each fragment:

$(".day_num").each(function () {
    let $this = $(this);
    let day = $this.attr("data-date");
    let date = $this.attr("date");
    let fullDate = day + " " + date;
    $this.find("div").attr("full-date", fullDate);
});
Related