I have to set one src to an image object. Then I change it.
But if I add something to the element (content of element), such as
meaning.innerHTML += ")";
(where meaning is parent element of image), then if change the src of object it won’t affect the document.
Example: http://jsfiddle.net/WcnCB/3/
Could you explain me why it happens, and how to fix it?
meaning.innerHTML += ')';does more than you think. Visually it just appends a)character, but behind the scenes what happens is:So, you’re first converting the DOM to a string representation (HTML), then adding a
)character, and finally have convert it back from HTML to the DOM. All elements the HTML represents are created again, andmeaningis replaced by those new elements. So your old one is distroyed.The simplest solution is to use
createTextNode: http://jsfiddle.net/WcnCB/4/.