Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

Setting, getting, and removing data attributes vanilla javascript

// setting data-foo
var el = document.querySelector('div');
el.setAttribute('data-foo', 'Hello World!');

// getting data-foo
var el = document.querySelector('img');
console.log(el.getAttribute('data-foo'))

// removing data-foo
el.removeAttribute('data-foo');
Source by plainjs.com #
 
PREVIOUS NEXT
Tagged: #removing #data #attributes #vanilla #javascript
ADD COMMENT
Topic
Name
7+9 =