Skip to content

Instantly share code, notes, and snippets.

@niloc132
Created February 4, 2014 18:17
Show Gist options
  • Save niloc132/8809252 to your computer and use it in GitHub Desktop.
Save niloc132/8809252 to your computer and use it in GitHub Desktop.
In non-IE browsers, changing the innerHTML of an element detaches its children, but leaves them intact. In IE, it also clears the innerhtml property of all children, detaching all descendants.
public class HowDoIInnerHtml implements EntryPoint {
@Override
public void onModuleLoad() {
Element body = Document.get().getBody();
DivElement newDiv = Document.get().createDivElement();
newDiv.setClassName("foo");
newDiv.setInnerText("Text in the div");
SpanElement newSpan = Document.get().createSpanElement();
newDiv.appendChild(newSpan);
body.appendChild(newDiv);
//page shows "Text in the div"
Window.alert("newDiv.innerHTML? " + newDiv.getInnerHTML() +
"\nspan is in div? " + (newSpan.getParentElement() == newDiv));
//alert shows "Text in the div <span></span>", true
body.setInnerHTML("");
Window.alert("newDiv.innerHTML? " + newDiv.getInnerHTML() +
"\nspan is in div? " + (newSpan.getParentElement() == newDiv));
//alert shows "", false ???
body.appendChild(newDiv);
//page shows blank
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment