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
  • 675
  • 5
  • 23
  • 42

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
  • 766
  • 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,641
  • 4
  • 16
  • 34