Created
July 18, 2014 22:24
-
-
Save svapreddy/b4c6022bdc73049d8094 to your computer and use it in GitHub Desktop.
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
/* | |
Here are some methods that work very efficiently and acts as fallback to many fast ass DOM libraries | |
*/ | |
document.querySelector('selector'); // selector is same as in querySelectorAll() | |
/* | |
Usage: | |
var firstP = document.querySelector('p'); - It gives first 'p' element in Document. | |
var firstPinsideDiv = someDiv.querySelector('p'); - It gives first 'p' element in someDiv. | |
It is an alternative for: | |
var firstP = document.querySelectorAll('p')[0]; | |
This is an alternative for jQuery way of selecting first Element: | |
var firstP = $('p').first(); | |
or | |
var firstPinsideDiv = $(someDiv).find('p').first(); | |
*/ | |
element.previousElementSibling // Avoids whitespace problem, so you don't need dirty hacks to find non whitespace prev sibling | |
element.nextElementSibling // Avoids whitespace problem, so you don't need dirty hacks to find non whitespace next sibling | |
element.firstElementChild // Avoids whitespace from selecting first child of some parent Element | |
element.insertAdjascentHTML // Excellent way of appending HTML comntent to any element. | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment