Skip to content

Instantly share code, notes, and snippets.

@svapreddy
Created July 18, 2014 22:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save svapreddy/b4c6022bdc73049d8094 to your computer and use it in GitHub Desktop.
Save svapreddy/b4c6022bdc73049d8094 to your computer and use it in GitHub Desktop.
/*
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