I am doing ajax. At a certain point, I assign someproperty to a DOM object selected by id (suppose it is 12345), and I confirm that the value has been assigned by using alert():
window.document.getElementById('12345').someproperty = true;
alert(window.document.getElementById('12345').someproperty);
At this point, the alert correctly shows true. Then, at some point later, I invoke a javascript command that looks up the value of someproperty for the object:
alert(window.document.getElementById('12345').someproperty);
and this time, it shows undefined. Why is the value not defined?
I have a feeling that you’re doing some nasty
.innerHTMLsomewhere between your to lookups of.someproperty.Assigning to
.innerHTMLdestroys the current DOM, and replaces it with a new DOM that is obtained by parsing the HTML string you provided. So I’m guessing you’re destroying an entire section, and replacing it with a nearly identical new section. This naturally wipes out the stateful information in the original DOM.You should modify the individual DOM elements that need updating instead of wiping them out entirely using
.innerHTML.