0

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?

user6824563
  • 735
  • 6
  • 25
  • 47

2 Answers2

3

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')
shotor
  • 763
  • 6
  • 17
1

you can use .attr() to set the value of a specific attribute of an element, like this:

$('#test').attr('data-sku', 'value');
Dij
  • 9,761
  • 4
  • 18
  • 35