<p>I need to copy data values from one element to another, but jQuery's clone() method doesn't clone the data. And I can't iterate over the data either:</p>

<pre><code>element.data().each
</code></pre>

<p>because <code>data()</code> is a function and not a jQuery object. It seems I have to keep a separate list of attribute names and reference those but that seems too hacky. So how can I do either of these:</p>

<p>a) Iterate over data items<br>
OR<br>
b) <code>clone()</code> an element with its data.</p>

To give another alternative, i.e. instead of cloning the whole object you can copy the data object to a new array containing name/value pairs followingly:
```
```
function getOriginalElementData(domElementJQueryObject){
    var originalElementData = new Array();
    $.each(domElementJQueryObject.data(),function(name,value) {
       originalElementData.push({"name":name,"value":value});
    });

    return originalElementData;
}
```
```
To restore the data to another object:
```
```
function restoreOriginalElementData(domElementJQueryObject,originalElementData){
    for(var i=0;i<originalElementData.length;i++){
        domElementJQueryObject.data(originalElementData[i].name,originalElementData[i].value);
    }
}
```
```
The part to iterate through the data items is copied from this answer:
jQuery loop through data() object