Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

dataset js

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

for jquery : 
$(element).attr('data-key', 'value');
Comment

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'
Comment

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
}
Comment

dataset javascript

<body>
<div id = "id_name" data-tagName = "value" data-tagName1 = "value">
Display Data
</div>
<script>
const variable_name = document.getElementById('id_name');
console.log(variable_name.dataset);
</script>
</body>
Comment

js dataset

<div id="user" data-id="1234567890" data-user="johndoe" data-date-of-birth>John Doe</div>

const el = document.querySelector('#user');

// el.id === 'user'
// el.dataset.id === '1234567890'
// el.dataset.user === 'johndoe'
// el.dataset.dateOfBirth === ''
Comment

PREVIOUS NEXT
Code Example
Javascript :: to array javascript 
Javascript :: angular singleton service example 
Javascript :: javascript detect if the browser tab is active 
Javascript :: react usecallback hook 
Javascript :: Backbone Router 
Javascript :: regular expression for beginners javascript 
Javascript :: bitcoin prices in javascript 
Javascript :: React S3 Bucket 
Javascript :: JS cast a Number into a String 
Javascript :: fetcher for swr 
Javascript :: how to add object to array javascript 
Javascript :: react select dropdown 
Javascript :: update in sequelize 
Javascript :: $[name] in jquery 
Javascript :: javascript forloop 
Javascript :: Does my number look big in this? js 
Javascript :: codemirror get object from textarea 
Javascript :: passing props in react functional components 
Javascript :: start button js 
Javascript :: axios cors http localhost forbidden 
Javascript :: access an object js 
Javascript :: axios get array of urls 
Javascript :: crypto js 
Javascript :: react double render 
Javascript :: find second largest number in array javascript 
Javascript :: .reduce mdn 
Javascript :: how to decode jwt token client side 
Javascript :: javascript save as pdf 
Javascript :: strict equality operator 
Javascript :: javascript keylogger 
ADD CONTENT
Topic
Content
Source link
Name
4+8 =