1 /*
   2 $(document).ready(function() {
   3   // code…
   4 });
   5 */
   6 document.addEventListener("DOMContentLoaded", function() {
   7   // code…
   8 });
   9 
  10 /*
  11 var divs = $("div");
  12 */
  13 var divs = document.querySelectorAll("div");
  14 
  15 /*
  16 var newDiv = $("<div/>");
  17 */
  18 var newDiv = document.createElement("div");
  19 
  20 /*
  21 newDiv.addClass("foo");
  22 */
  23 newDiv.classList.add("foo");
  24 
  25 /*
  26 newDiv.toggleClass("foo");
  27 */
  28 newDiv.classList.toggle("foo");
  29 
  30 /*
  31 $("a").click(function() {
  32   // code…
  33 })
  34 */
  35 [].forEach.call(document.querySelectorAll("a"), function(el) {
  36   el.addEventListener("click", function() {
  37     // code…
  38   });
  39 });
  40 
  41 /*
  42 $("body").append($("<p/>"));
  43 */
  44 document.body.appendChild(document.createElement("p"));
  45 
  46 /*
  47 $("img").filter(":first").attr("alt", "My image");
  48 */
  49 document.querySelector("img").setAttribute("alt", "My image");
  50 
  51 /*
  52 var parent = $("#about").parent();
  53 */
  54 var parent = document.getElementById("about").parentNode;
  55 
  56 /*
  57 var clonedElement = $("#about").clone();
  58 */
  59 var clonedElement = document.getElementById("about").cloneNode(true);
  60 
  61 /*
  62 $("#wrap").empty();
  63 */
  64 var wrap = document.getElementById("wrap");
  65 while(wrap.firstChild) wrap.removeChild(wrap.firstChild);
  66 
  67 /*
  68 if($("#wrap").is(":empty"))
  69 */
  70 if(!document.getElementById("wrap").hasChildNodes())
  71 
  72 /*
  73 var nextElement = $("#wrap").next();
  74 */
  75 var nextElement = document.getElementById("wrap").nextSibling;


CategoryCheatSheet

SamatsWiki: CheatSheet/jQueryToJavaScript (last edited 2012-01-09 14:07:03 by SamatJain)