close

How can I get the values of data attributes in JavaScript code?

Hello Guys, How are you all? Hope You all Are Fine. Today we are going to learn How can I get the values of data attributes in JavaScript code? in javascript. So Here I am Explain to you all the possible methods here.

Without wasting your time, Let’s start This Article.

How can I get the values of data attributes in JavaScript code ?

  1. How can I get the values of data attributes in JavaScript code ?

    can I get the values of data attributes in JavaScript code Because the dataset property wasn't supported by Internet Explorer until version 11, you may want to use getAttribute() instead:

  2. can I get the values of data attributes in JavaScript code

    can I get the values of data attributes in JavaScript code Because the dataset property wasn't supported by Internet Explorer until version 11, you may want to use getAttribute() instead:

Method 1

Because the dataset property wasn’t supported by Internet Explorer until version 11, you may want to use getAttribute() instead:

document.getElementById("the-span").addEventListener("click", function(){
  console.log(this.getAttribute('data-type'));
});

Method 2

You can access it as

element.dataset.points

etc. So in this case: this.dataset.points

Summery

It’s all About this article. Hope all method helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you? Thank You.

Leave a Comment