how to copy an object with some elements?
I want to copy this object, but each elm still refers to the same DOM element?
var fields = {
account_id_ : {
name : Lang.get('HDL_ACCOUNT'),
width : 60,
elm : $('<input class="inp" type="text" style="text-align:right" />'),
value : '',
focus : true
},
name : {
name : Lang.get('HDL_NAME'),
width : null,
elm : $('<input class="inp" type="text" />'),
value : ''
}
};
You can just loop through and
.clone()the jQuery object (and the elements, 1 in this case, that it references. A very concise method would look like this:Now
fields2has it’s own elements, you can test it here, compare it to here which doesn’t run the clone line, and references/re-appends the same elements, instead of clones in the first example.For clarity, a raw javascript version of the clone loop would look like this: