Last active
August 29, 2015 14:09
-
-
Save VincentHelwig/eefddc90557093d64aca to your computer and use it in GitHub Desktop.
Vanilla JS
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
// FROM : http://playground.deaxon.com/js/vanilla-js/ | |
// and complement : http://putaindecode.fr/posts/js/de-jquery-a-vanillajs/ | |
Events | |
$(document).ready(function() { }); document.addEventListener("DOMContentLoaded", function() { }); | |
$("a").click(function() { }) [].forEach.call(document.querySelectorAll("a"), function(el) { | |
el.addEventListener("click", function() {}); | |
}); | |
Selectors | |
var divs = $("div"); var divs = document.querySelectorAll("div"); | |
var newDiv = $("<div/>"); var newDiv = document.createElement("div"); | |
Classes | |
newDiv.addClass("foo"); newDiv.classList.add("foo"); | |
newDiv.toggleClass("foo"); newDiv.classList.toggle("foo"); | |
Manipulation | |
$("body").append($("<p/>")); document.body.appendChild(document.createElement("p")); | |
var clonedElement = $("#about").clone(); var clonedElement = document.getElementById("about").cloneNode(true); | |
$("#wrap").empty(); var wrap = document.getElementById("wrap"); | |
while(wrap.firstChild) wrap.removeChild(wrap.firstChild); | |
Attributs | |
$("img").filter(":first").attr("alt", "My image"); document.querySelector("img").setAttribute("alt", "My image"); | |
Navigation | |
var parent = $("#about").parent(); var parent = document.getElementById("about").parentNode; | |
if($("#wrap").is(":empty")) if(!document.getElementById("wrap").hasChildNodes()) | |
var nextElement = $("#wrap").next(); var nextElement = document.getElementById("wrap").nextSibling; | |
AJAX | |
GET | |
var httpRequest = new XMLHttpRequest() | |
httpRequest.onreadystatechange = function (data) {} | |
httpRequest.open('GET', url) | |
httpRequest.send() | |
POST | |
var httpRequest = new XMLHttpRequest() | |
httpRequest.onreadystatechange = function (data) {} | |
httpRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded') | |
httpRequest.open('POST', url) | |
httpRequest.send('username=' + encodeURIComponent(username)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment