I have javascript variable obj which represents an element in the DOM and I wish to change its ID. I have attempted to do this by the following, which also illustrates that it does not work!
obj.id = "newID";
alert(obj.id); // this gives me newID as required
var element = document.getElementById("newID");
if (element != null) { alert("Hooray"); // this alert never gets displayed! }
What is wrong with my code above that means that the id seems to be changed but not picked up in the DOM? Many thanks in advance.
The code you show does work; the problem is probably in the code which looks up and assigns
obj. My guess is that this is just a javascript object, not a part of the DOM tree.