Answers for "js dataset"

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
2

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'
Posted by: Guest on December-07-2020
0

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
}
Posted by: Guest on August-24-2021

Code answers related to "Javascript"

Browse Popular Code Answers by Language