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'); }); 

http://jsfiddle.net/44du5/


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -