Skip to content

Instantly share code, notes, and snippets.

@itsallrelative
Last active August 29, 2015 14:12
Show Gist options
  • Select an option

  • Save itsallrelative/b5570cd5ff7b4e4ecf0a to your computer and use it in GitHub Desktop.

Select an option

Save itsallrelative/b5570cd5ff7b4e4ecf0a to your computer and use it in GitHub Desktop.
Pure JavaScript solutions without jQuery
//Document Ready
//jQuery
$(document).ready(function(){
//other jQuery here
});
//Good ol' fashion JS IE9+
document.addEventListener('DOMContentLoaded', function(){
});
//Event Listeners
//No longer shall we use element.attachEvent!!!
//jQuery
var $thing = $('.some-element');
$thing.on('click', function(){
alert("You don't need no stinking jQuery!");
});
//vanilla.js
var thing = document.querySelectorAll('.some-element');
thing.addEventListener("click",alertMessage,false);
function alertMessage(){
alert("You don't need no stinking jQuery!");
}
//Getting a DOM Element
//jQuery
var $thing = $('.some-element-with-a-class');
var $thing2 = $('#some-element-with-an-id');
var $thing3 = $('p'); //returns array of all p elements
//The cross browser compatible IE9+ method
var thing = document.querySelectorAll('.some-element-with-a-class');
var thing2 = document.getElementById('some-element-with-an-id');
var thing3 = document.getElementsByTagName('p'); //returns array of all p elements
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment