Answers for "how to can i get custom 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
7

dataset js

for javascript : 
webappData.getAttribute('data-id');
document.querySelector('#elementId').dataset.id = 'value';

for jquery : 
$(element).attr('data-key', 'value');
Posted by: Guest on June-30-2020
0

how to can i get custom data attribute value in javascript

example <button data-keyname = "a'></button>

let variable = element.dataset.keyname;
or
let varibale = element.['keyname'];
or
let variable = element.getAttribute('data-keyname');
Posted by: Guest on December-31-2020
13

data attribute html

<!-- data-* attritubes can be used on any html element -->
<div data-my-custom-data="true"></div>
Posted by: Guest on October-29-2020

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

Code answers related to "Javascript"

Browse Popular Code Answers by Language