Answers for "how to get data attribute value in javascript"

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
0

javascript get data-id attribute

//get data-id attribute in plain Javascript
var element = document.getElementById('myDivID');
var dataID = element.getAttribute('data-id');

//get data-id using jQuery
var dataID = $('myDivID').data('data-id');
Posted by: Guest on July-31-2019
5

html javascript find data attribute

//javascript get html data attribute
<button data-id="1" >Click</button>
<button data-id="2" >Click</button>
<button data-id="3" >Click</button>
const btns=document.querySelectorAll('button[data-id]');
[...btns].forEach(btn => console.log(btn.getAttribute('data-id')))
Posted by: Guest on July-25-2020
0

javascript get data attribute of selected option

function check_status(obj) {
  var uid = obj.options[obj.selectedIndex].getAttribute('data');
  alert(uid);
}
Posted by: Guest on September-09-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

Code answers related to "how to get data attribute value in javascript"

Code answers related to "Javascript"

Browse Popular Code Answers by Language