Last active
April 12, 2025 09:10
-
-
Save joyrexus/5322252 to your computer and use it in GitHub Desktop.
Revisions
-
joyrexus revised this gist
Apr 5, 2013 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -85,20 +85,20 @@ newDiv.classList.toggle("foo"); ```javascript $("a").click(function() { // code }) [].forEach.call(document.querySelectorAll("a"), function(el) { el.addEventListener("click", function() { // code }); }); $(document).ready(function() { // code }); document.addEventListener("DOMContentLoaded", function() { // code }); ``` -
joyrexus revised this gist
Apr 5, 2013 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,5 +1,5 @@ jQuery vs native JS =================== ## Selecting Elements -
joyrexus created this gist
Apr 5, 2013 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,104 @@ JQ vs native JS =============== ## Selecting Elements ```javascript var divs = $("div"); var divs = document.querySelectorAll("div"); var parent = $("#about").parent(); var parent = document.getElementById("about").parentNode; var nextElement = $("#wrap").next(); var nextElement = document.getElementById("wrap").nextSibling; ``` ## Setting Attributes ```javascript $("img").filter(":first").attr("alt", "My image"); document.querySelector("img").setAttribute("alt", "My image"); ``` ## Creating Elements ```javascript var newDiv = $("<div/>"); var newDiv = document.createElement("div"); $("body").append($("<p/>")); document.body.appendChild(document.createElement("p")); ``` ## Cloning Elements ```javascript var clonedElement = $("#about").clone(); var clonedElement = document.getElementById("about").cloneNode(true); ``` ## Removing Elements ```javascript $("#wrap").empty(); var wrap = document.getElementById("wrap"); while(wrap.firstChild) wrap.removeChild(wrap.firstChild); ``` ## Testing for child nodes ```javascript if($("#wrap").is(":empty")) if(!document.getElementById("wrap").hasChildNodes()) ``` ## Adding Classes ```javascript newDiv.addClass("foo"); newDiv.classList.add("foo"); ``` ## Toggling Classes ```javascript newDiv.toggleClass("foo"); newDiv.classList.toggle("foo"); ``` ## Adding Events ```javascript $("a").click(function() { // code… }) [].forEach.call(document.querySelectorAll("a"), function(el) { el.addEventListener("click", function() { // code… }); }); $(document).ready(function() { // code… }); document.addEventListener("DOMContentLoaded", function() { // code… }); ```