Skip to content Skip to sidebar Skip to footer

Maintain Reference To Element After Inserting Into Dom

I've traditionally used JavaScript as 'UI Glue', and have (regrettably) been one of those that looked at JS as a 'toy'. However, I've changed course and am finding some real power

Solution 1:

var new_div = document.createElement('div');
new_div.id = 'foo';

EDIT: Just re-read your question, are you saying new_div doesn't work after an appendChild? I'm pretty sure it should. You're not adding it via innerHTML or something strange like that?

Maybe your var is falling out of scope where you are trying to read it. (ie, you are creating it in one function and reading it from another?). Try declaring the object globally.

Solution 2:

Your original, global, reference should still work fine. What are you trying to do with it? Do you have any example code?

Solution 3:

Are you using a library or something that is wrapping a native DOM object?, this might be the problem...

Solution 4:

All objects are passed by reference in JavaScript, and HTMLNodes are no exception. Your original reference should always work until you assign it a different value. Sounds like you have a scope issue. Could we see the code?

Post a Comment for "Maintain Reference To Element After Inserting Into Dom"