jquery - pass an javascript object to DOM element data atribute -


if fetch data attributes 1 dom element effective way pass element? example of data attributes needs passed:

object {avatar: "...", lname: "...", fname: "...", username: "..."} 

you can use data method object, set properties in object data values:

$('#someelement').data(obj); 

you can duplicate data 1 element getting data object , use set data:

$('#someelement').data($('#sourceelement').data()); 

demo:

$('#div2').data($('#div1').data());  // show in stackoverflow snippet  document.write(json.stringify($('#div2').data()));
<div id="div1" data-avatar="42" data-lname="pan" data-fname="peter" data-username="peterpan07"></div>  <div id="div2"></div>    <script src="//code.jquery.com/jquery-1.11.2.min.js"></script>


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -