javascript - Replace the reference to the DOM object -
i want write javascript function replace 1 dom element one. need reference old element pointing new element after replacement.
i wrote following function purpose:
var replacedomelement = function (el1, el2) { var parent = el1.parentnode; if (!parent) return false; parent.replacechild(el2, el1); el1 = el2; return true; }
but doesn't work in way want. dom element replaced.
but statement el1 = el2
doesn't work. after execution of function el1 still pointing old element, has no parent now.
what correct way of changing references dom object inside function?
the following code works want:
var replacedomelement = function (el1, el2) { var parent = el1.parentnode; if (!parent) return false; parent.replacechild(el2, el1); return el2; } var element1 = document.getelementbyid("el1"); var element2 = document.getelementbyid("el2"); element1 = replacedomelement(element1, element2); alert(element1 === element2);
i.e. @ end element1 equal element2. difference return el2. here codepen showing example http://codepen.io/krasimir/pen/lcyfe
Comments
Post a Comment