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);
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);
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');
get data attribute javascript
// <div id="element" data-name="john"></div>
const el = document.querySelector('#element')
el.dataset.name // 'john'
dataset js
for javascript :
webappData.getAttribute('data-id');
document.querySelector('#elementId').dataset.id = 'value';
for jquery :
$(element).attr('data-key', 'value');
javascript dataset
//html file
<div id="user" data-user-id="2" data-user-date-of-birth="03/03/1993">
<p> Hello ! </p>
</div>
//js file
const user = document.getElementById('user')
console.log(user.dataset.userId) // => 2
console.log(user.dataset.userDateOfBirth) // => '03/03/1993'
dataset js
const el = document.querySelector('#user');
// el.id === 'user'
// el.dataset.id === '1234567890'
// el.dataset.user === 'johndoe'
// el.dataset.dateOfBirth === ''
// set a data attribute
el.dataset.dateOfBirth = '1960-10-03';
// Result: el.dataset.dateOfBirth === '1960-10-03'
delete el.dataset.dateOfBirth;
// Result: el.dataset.dateOfBirth === undefined
if ('someDataAttr' in el.dataset === false) {
el.dataset.someDataAttr = 'mydata';
// Result: 'someDataAttr' in el.dataset === true
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us