I have a custom parameter in my div and I want to insert a value into this parameter using JS or JQuery.
<div id='test' data-sku=""></div>
Using JS or JQuery, how can I change/insert a value in data-sku?
I have a custom parameter in my div and I want to insert a value into this parameter using JS or JQuery.
<div id='test' data-sku=""></div>
Using JS or JQuery, how can I change/insert a value in data-sku?
This will do the trick:
jQuery (Check out the documentation for more usage examples: http://api.jquery.com/attr/):
$('#test').attr('data-sku', 'my value')
Vanilla JavaScript:
document.querySelector('#test').setAttribute('data-sku', 'my value')
you can use .attr()
to set the value of a specific attribute of an element, like this:
$('#test').attr('data-sku', 'value');