How do I access the date-weekday property of the div element with javascript?

1

How to access the date-weekday property of the div element with javascript?

<div style="display: none;" id="weekday" data-weekday="7"></div>
    
asked by anonymous 14.06.2018 / 14:27

2 answers

2

You can use .dataset :

const weekDay = document.getElementById('weekday').dataset.weekday;
console.log(weekDay); // 7
<div style="display: none;" id="weekday" data-weekday="7"></div>

Or read the attribute directly:

const weekDay = document.getElementById('weekday').getAttribute('data-weekday');
console.log(weekDay); // 7
<div style="display: none;" id="weekday" data-weekday="7"></div>
    
14.06.2018 / 14:35
0

To get an attribute of your div just use getAttribute("data-weekday"); specified in w3schools. Follow the demo.

var x = document.getElementById("weekday").getAttribute("data-weekday");
alert(x);
<div style="display: none;" id="weekday" data-weekday="7"></div>

With Jquery

var x = $("#weekday").attr("data-weekday");
alert(x);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divstyle="display: none;" id="weekday" data-weekday="7"></div>
    
14.06.2018 / 14:32