Answers for "javascript html data attribute"

12

js get data attribute

var element = document.querySelector('.element');
var dataAttribute = element.getAttribute('data-name');
// replace "data-name" with your data attribute name

console.log(dataAttribute);
Posted by: Guest on February-03-2020
5

get data attribute javascript

// <div id="element" data-name="john"></div>

const el = document.querySelector('#element')
el.dataset.name // 'john'
Posted by: Guest on November-02-2020
1

html javascript data attributes

/* index.html
<div
  id="test-div"
  data-first-name="Tom"
  data-last-name="Mot"
  data-active
></div>
*/

// Reading
const div = document.getElementById("test-div");
console.log(div.dataset);
/*
{
  active: ""
  firstName: "Tom"
  lastName: "Mot"
}
*/

// Writing
const div = document.getElementById("test-div");
div.dataset.test = "Hello World!";
console.log(div.dataset.test); // Hello World
Posted by: Guest on July-10-2021
5

javascript get data attribute value

const article = document.querySelector('#electric-cars');
 
article.dataset.columns // "3"
article.dataset.indexNumber // "12314"
article.dataset.parent // "cars"
Posted by: Guest on April-15-2020
0

html data attribute

<a data-color="blue">
  	This tag contains a user-defined color attribute
</a>

<div data-link="https://github.com/k-vernooy">
	This tag contains a data attribute with a link
</div>
Posted by: Guest on February-22-2020

Code answers related to "javascript html data attribute"

Code answers related to "Javascript"

Browse Popular Code Answers by Language