Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
125
rated 0 times [  126] [ 1]  / answers: 1 / hits: 16214  / 14 Years ago, fri, september 24, 2010, 12:00:00

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.


More From » dom

 Answers
35

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.


[#95511] Wednesday, September 22, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
briarc

Total Points: 402
Total Questions: 85
Total Answers: 114

Location: El Salvador
Member since Sun, Sep 12, 2021
3 Years ago
;