Created
June 9, 2013 14:16
-
-
Save jswhisperer/5743704 to your computer and use it in GitHub Desktop.
JS native equivilant to DOM Manipulation with jQuery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* Append HTML elements 8/ | |
| /* jQuery */ | |
| $(document.body).append("<div id='myDiv'><img src='im.gif'/></div>"); | |
| /* CRAPPY native equivalent */ | |
| document.body.innerHTML += "<div id='myDiv'><img src='im.gif'/></div>"; | |
| /* MUCH BETTER native equivalent */ | |
| var frag = document.createDocumentFragment(); | |
| var myDiv = document.createElement("div"); | |
| myDiv.id = "myDiv"; | |
| var im = document.createElement("img"); | |
| im.src = "im.gif"; | |
| myDiv.appendChild(im); | |
| frag.appendChild(myDiv); | |
| document.body.appendChild(frag); | |
| /* Prepend HTML elements */ | |
| /* same as above except for last line */ | |
| document.body.insertBefore(frag, document.body.firstChild); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment