javascript - jQuery Change Value Using Data Attribute -
i have multiple elements that'll have same data attributes different values, how can jquery change value?
below example html.
<div data-one="test" data-two="test2"></div> <div data-one="testing" data-two="hello"></div> <div data-one="yo" data-two="test3"></div>
by default, value of div
data-one
when class active
on body
tag, change values data-two
value.
i thought storing values variable easier although divs don't have id's , they're scattered around in dom makes difficult.
so far had this:
if($('body').hasclass('active')) { $('div').html($(div).data('two')); }
you can use html
method.
var has = $('body').hasclass('active'); $('div').html(function() { return has ? $(this).data('two') : $(this).data('one'); });
Comments
Post a Comment