jquery - store data name equal each class name -
i have html structure below , have store each container inner div text val data, i'm coding below js, works fine feel not smart, because these code kind doing samething store data name equal class name, have type , check typo... possible more simple samething??
<div class="container1"> <div class="aq">text</div> <div class="be">text</div> <div class="co">text</div> <div class="dp">text</div> ... </div> <div class="container1"> <div class="aq">text</div> <div class="be">text</div> <div class="co">text</div> <div class="dp">text</div> ... </div> <div class="container2"> <div class="aa">text</div> <div class="bd">text</div> <div class="cs">text</div> <div class="dw">text</div> ... </div> <div class="container2"> <div class="aa">text</div> <div class="bd">text</div> <div class="cs">text</div> <div class="dw">text</div> ... </div>
js
$(".container1").each(function(){ $(this).data('aq', $(this).find('.aq').text()); $(this).data('be', $(this).find('.be').text()); $(this).data('co', $(this).find('.co').text()); $(this).data('dp', $(this).find('.dp').text()); }); $(".container2").each(function(){ $(this).data('aa', $(this).find('.aa').text()); $(this).data('bd', $(this).find('.bd').text()); $(this).data('cs', $(this).find('.cs').text()); $(this).data('dw', $(this).find('.dw').text()); });
$('[class^=container] div').each(function(){ $(this).parent().data($(this).prop('class'), $(this).text()); });
Comments
Post a Comment