# Essential Cheat Sheet: Convert jQuery to JavaScript

In this cheat sheet, you can find the most commonly used jQuery-to-JavaScript equivalents with syntax, code examples, and links to documentation.

Even though jQuery is still a popular JavaScript library and has a place in web development, native web APIs have evolved a lot in recent years. As modern browsers support a wide range of JavaScript functionalities, we no longer need a third-party library to manipulate the DOM with relative ease. As a result, many frontend developers have decided to move from jQuery to JavaScript to decrease the number of dependencies and thus page load times.

## jQuery vs. JavaScript

jQuery is a JavaScript library that helps web developers manipulate the document object model (DOM) to dynamically react to user actions and state changes. Its biggest advantage is that it lets you easily select any DOM element on the page and add user actions to it by using the “dot notation”, for instance:

 1 /* Adds the .active class to the toggle button */  2 $('#toggle').addClass('active');  Native JavaScript is more verbose, you can’t just add actions to any DOM element–first you need to pass it to a variable, then you can perform different actions on it. For instance, this is how the code above looks in JavaScript:  1 let toggle = document.querySelector('#toggle');  2 toggle.classList.add('active');  Even though JavaScript is more verbose, you don’t have to use a third-party library, as the DOM API (that you call with your JavaScript code) is built into your web browser. As a result, you’ll have one less dependency and in most cases, your page will also load faster than with jQuery. However you need to judge for yourself if it’s worth moving to native JavaScript as you’ll need to write more code to achieve the same result. So, the trade-off between jQuery and JavaScript is more custom-written code vs. fewer dependencies. Now, let’s see the 30 most commonly used jQuery to JavaScript equivalents. ## 1. Select Elements jQuery: The jQuery() function, or $() in short, is the global function in jQuery that you can use to select any element in the DOM.

 1 /* Syntax */  2 3 jQuery();  4 $(); // shortcut   1 /**  2  * Example  3  *  4  * Selects all the links among the descendants of the 'my-class' class  5  */  6   7 jQuery('.my-class a');  8 $('.my-class a'); 

See more in the jQuery API docs: jQuery() global function.

JavaScript:

 1 /* Syntax */  2 3 Document.querySelectorAll(); 
 1 /* Example */  2 3 document.querySelectorAll('.my-class a'); 

See more in the DOM API docs: .querySelectorAll() method.

## 2. Select First Element

jQuery:

 1 /* Syntax */  2 3 .first(); 
 1 /**  2  * Example  3  *  4  * Selects the first link among the descendants of the 'my-class' class  5  */  6 7 $('.my-class a').first();  See more in the jQuery API docs: first() method. JavaScript:  1 /* Syntax */  2 3 Document.querySelector();   1 /* Example */  2 3 document.querySelector('.my-class a');  See more in the DOM API docs: .querySelector() method. ## 3. Find Elements jQuery:  1 /* Syntax */  2 3 .find();   1 /**  2  * Example  3  *  4  * Find all the span tags that are descendants of links within  5  * the 'my-class' class  6  *  7  * Note: searches for all descendants not just for children  8  */  9   10 $('.my-class a').find('span');  11 12 $('.my-class a').find('span').css('color', 'red'); // for testing  See more in the jQuery API docs: .find() method. JavaScript:  1 /* Syntax */  2 3 // to find the first element (also if there's only one)  4 Document.querySelector();  5 6 // to find all elements  7 Document.querySelectorAll();   1 /**  2  * Example  3  *  4  * At first querySelectorAll() returns a NodeList, so we have to loop  5  * through it to find all the span tags we want  6  *  7  * For the sake of testing, I made the selected elements red,  8  * you can find the 'style.color' property below in this cheat sheet  9  */  10 11 // finds all '.my-class a'  12 let nodes = document.querySelectorAll('.my-class a');  13 14 // loops through all '.my-class a'  15 for (let i = 0; i < nodes.length; i++) {  16   17  // checks if the individual '.my-class a' element has a  18  // 'span' descendant to avoid 'undefined' error  19  if (nodes[i].querySelector('span')) {  20   21  // colors span tags that are desdendants of '.my-class a'  22  nodes[i].querySelector('span').style.color = 'red';  23   24  }  25 26 }  See code demo: See more in the DOM API docs: .querySelector() method.querySelectorAll() method. ## 4. Select Children jQuery:  1 /* Syntax */  2 3 .children();  4 .children('selector');   1 /**  2  * Example  3  *  4  * (1) Finds all the children of all '.my-class a' elements on the age  5  *  6  * (2) Finds all the 'span' elements that are the children of  7  * any '.my-class a' element on the page  8  *  9  * Note: searches only for children (first-level of descendants)  10  */  11   12 $('.my-class a').children();  13 14 $('.my-class a').children('span');  15 16 $('.my-class a').children('span').css('color', 'blue'); // for testing 

See more in the jQuery API docs: .children() method.

JavaScript:

 1 /* Syntax */  2 3 ParentNode.children; 
 1 /**  2  * Example  3  *  4  * 2nd example of the jQuery version above, plus makes the selected span  5  * tags blue for the sake of easy testing  6  *  7  * for 1st example, only leave out the if check at the end (we need this  8  * because the JS version is not a method but a property, so we  9  * need to check which children are 'span')  10  *  11  */  12 13 // selects all the elements with 'my-class a' on the page  14 let items = document.querySelectorAll('.my-class a');  15 16 // loops through all the elements with '.my-class a'  17 for (let i = 0; i < items.length; i++) {  18   19  // finds the children of the current '.my-class a' element  20  let kids = items[i].children;  21   22  // loops through the children of the current '.my-class a' element  23  for (let j = 0; j < kids.length; j++) {  24   25  // checks if the child element is a span tag  26  if (kids[j].tagName == 'SPAN') {  27   28  kids[j].style.color = 'blue';  29   30  }  31   32  }  33 34 } 

See test code:

See more in the DOM API docs: .children property – remember that while the JavaScript version is a property, the jQuery version was a method with a parenthesis after it.

## 5. Select Parent

jQuery:

 1 /* Syntax */  2 3 .parent(); 
 1 /**  2  * Example  3  *  4  * Selects the parent elements of ALL elements with 'my-class a'  5  * on the page  6  */  7   8 $('.my-class a');  See more in the jQuery API docs: .parent() method JavaScript:  1 /* Syntax */  2 3 Node.parentNode;   1 /**  2  * Example  3  *  4  * Selects the parent of the FIRST element with 'my-class a' on the page  5  * (for the sake of less repetition)  6  *  7  * For looping through all '.my-class a' elements, use the looping  8  * solution and querySelectorAll() from the two examples above.  9  */  10 11 let item = document.querySelector('.my-class a');  12 item.parentNode;  See more in the DOM API docs: .parentNode property. ## 6. Select Siblings jQuery:  1 /* Syntax */  2 3 .siblings();   1 /**  2  * Example  3  *  4  * Selects the siblings of ALL elements with the 'find-siblings'  5  * class on the page  6  */  7   8 $('.find-siblings').siblings(); 

See more in the jQuery API docs: .siblings() method.

JavaScript:

 1 /* Syntax */  2 3 Node.parentNode.querySelectorAll(":not(#elem)"); 
 1 /**  2  * Example  3  *  4  * Selects the siblings of the FIRST element with the  5  * 'find-siblings' class  6  *  7  * For looping through all 'find-siblings' elements, see examples #3 and #4  8  *  9  * the ':scope' pseudoclass is necessary for preventing the child  10  * elements of 'item' from being selected (otherwise querySelectorAll()  11  * searches through all descendants of 'item', with ':scope >' it loops  12  * through just the first level)  13  *  14  */  15 16 let item = document.querySelector('.find-siblings');  17 let siblings = item.parentNode.querySelectorAll(':scope > :not(.find-siblings)'); 

See test demo:

See more in the DOM API docs: .parentNode property, .querySelectorAll() method, :scope pseudo-class (see 'Direct Children' section).

## 7. Select Next Sibling

jQuery:

 1 /* Syntax */  2 3 .next(); 
 1 /**  2  * Example  3  *  4  * Selects the next siblings of all elements with 'my-class a'  5  * on the page  6  */  7   8 $('.my-class a').next();  See more in the jQuery API docs: .next() method. JavaScript:  1 /* Syntax */  2 3 NonDocumentTypeChildNode.nextElementSibling;   1 /**  2  * Example  3  *  4  * For declaring the 'item' variable by selecting elements with  5  * 'my-class a' on the page, see examples #3, #4, #5  6  */  7 item.nextElementSibling;  See more in the DOM API docs: .nextElementSibling property. ## 8. Select Previous Sibling jQuery:  1 /* Syntax */  2 3 .prev();   1 /**  2  * Example  3  *  4  * Selects the previous siblings of all elements with 'my-class a'  5  * on the page  6  */  7   8 $('.my-class a').prev(); 

See more in the jQuery API docs: .prev() method.

JavaScript:

 1 /* Syntax */  2 3 NonDocumentTypeChildNode.previousElementSibling; 
 1 /**  2  * Example  3  *  4  * For declaring the 'item' variable by selecting elements with  5  * 'my-class a' on the page, see examples examples #3, #4, #5  6  */  7 item.previousElementSibling; 

See more in the DOM API docs: .previousElementSibling property.

## 9. Find the Closest Matching Element

jQuery:

 1 /* Syntax */  2 .closest('selector-name'); 
 1 /**  2  * Example  3  *  4  * Checks each 'my-class' element and all of its parent elements,  5  * then returns the first paragraph element it finds.  6  */  7 $('.my-class').closest('p');  See more in the jQuery API docs: .closest() method. JavaScript:  1 /* Syntax */  2 Element.closest();   1 /** Example  2  *  3  * Checks item and all of its parents and returns the first div it finds  4  *  5  * For declaring the 'item' variable, see examples #3, #4, #5  6  */  7 item.closest('div');  See test demo: See more in the DOM API docs: .closest() method. ## 10. Add Class jQuery:  1 /* Syntax */  2 3 .addClass();   1 /**  2  * Example  3  *  4  * Adds the 'second-class' class to every 'my-class' element  5  *  6  */  7 $('.my-class').addClass('second-class'); 

See more in the jQuery API docs: .addClass() method.

JavaScript:

 1 /* Syntax */  2 3 Element.classList.add(); 
 1 /**  2  * Example  3  *  4  * For declaring the 'item' variable by selecting elements with 'my-class'  5  * on the page, see examples examples #3, #4, #5  6  */  7 8 item.classList.add('second-class'); 

See more in the DOM API docs: .classList property and .add() method.

## 11. Remove Class

jQuery:

 1 /* Syntax */  2 3 .removeClass(); 
 1 /**  2  * Example  3  *  4  * (1) Removes the 'second-class' class from every 'my-class' element  5  *  6  * (2) Removes 'second-class', then add 'third-class' to  7  * every 'my-class' element  8  */  9 $('.my-class').removeClass('second-class');  10 $('.my-class').removeClass('second-class').addClass('third-class'); 

See more in the jQuery API docs: .removeClass() method.

JavaScript:

 1 /* Syntax */  2 3 Element.classList.remove(); 
 1 /**  2  * Example  3  *  4  * For declaring the 'item' variable by selecting elements with 'my-class'  5  * on the page, see examples examples #3, #4, #5  6  */  7   8 item.classList.remove('second-class');  9 10 // To use it together with add(), you need two separate statements  11 item.classList.remove('second-class');  12 item.classList.add('third-class'); 

See more in the DOM API docs: .classList property.remove() method.

## 12. Toggle Class

jQuery:

 1 /* Syntax */  2 3 .toggleClass(); 
 1 /** Example  2  *  3  * Adds the 'active' class to elements with 'my-class' if they don' have it  4  * remove it if they have it  5  *  6  */  7   8 $('.my-class').toggleClass('active');  See more in the jQuery API docs: .toggleClass() method. JavaScript:  1 /* Syntax */  2 3 Element.classList.toggle();   1 /**  2  * Example  3  *  4  * For declaring the 'item' variable, see examples #3, #4, #5  5  */  6   7 item.classList.toggle('active');  See more in the DOM API docs: .classList property.toggle() method. ## 13. Has Class jQuery:  1 /* Syntax */  2 3 .hasClass();   1 /**  2  * Example  3  *  4  * Checks if any element with 'my-class' has the 'active' class  5  *  6  * Returns true or false  7  *  8  * If there's at least one element with 'active' it's true,  9  * otherwise false.  10  *  11  */  12 13 $('.my-class').hasClass('active'); 

See more in the jQuery API docs: .hasClass() method.

JavaScript:

 1 /* Syntax */  2 3 Element.classList.contains(); 
 1 /**  2  * Example  3  *  4  * Similar to the jQuery version, this one also checks if any element  5  * in the whole classList has the 'active' class  6  *  7  * If at least one element has 'active', it's true, otherwise false  8  *  9  * For declaring the 'item' variable, see examples #3, #4, #5  10  */  11   12 item.classList.contains('active'); 

See more in the DOM API docs: .classList property, .contains() method.

## 14. Get Attribute

jQuery:

 1 /* Syntax */  2 3 .attr('attr-name'); 
 1 /**  2  * Example  3  *  4  * Returns the value of the href property of the FIRST occurrence of  5  * an element with 'my-class'  6  *  7  */  8   9 $('.my-class').attr('href');  See more in the jQuery API docs: .attr() method. JavaScript:  1 /* Syntax */  2 3 Element.getAttribute('attr-name');   1 /**  2  * Example  3  *  4  * For declaring the 'item' variable, see examples #3, #4, #5  5  */  6   7  item.getAttribute('href');  See more in the DOM API docs: .getAttribute() method. ## 15. Set Attribute jQuery:  1 /* Syntax */  2 3 .attr('attr-name', value);   1 /**  2  * Example  3  *  4  * Sets the value of the href property for ALL contact links that  5  * have the 'contact-link' class  6  *  7  */  8   9 $('.contact-link').attr('href', 'contact.html'); 

See more in jQuery API docs: .attr() method (you need to use the same .attr() method as for getting an attribute value, but with two parameters instead of one).

JavaScript:

 1 /* Syntax */  2 3 Element.setAttribute(); 
 1 /**  2  * Example  3  *  4  * For declaring the 'item' variable, see examples #3, #4, #5  5  */  6   7  item.setAttribute('href', 'contact.html'); 

See more in the DOM API docs: .setAttribute() method.

## 16. Remove Attribute

jQuery:

 1 /* Syntax */  2 3 .removeAttr('attr-name'); 
 1 /**  2  * Example  3  *  4  * Removes 'target' attributes from contact links  5  */  6   7 $('.contact-link').removeAttr('target');  See more in the jQuery API docs: .removeAttr() method. JavaScript:  1 /* Syntax */  2 3 Element.removeAttribute();   1 /**  2  * Example  3  *  4  * For declaring the 'item' variable, see examples #3, #4, #5  5  */  6   7  item.removeAttribute('target');  See more in the DOM API docs: .removeAttribute() method. ## 17. Append a New Child Element jQuery:  1 /* Syntax */  2   3 .append('html-string');   1 /**  2  * Example  3  *  4  * Appends an extra list element to the end of every ordered list on the page  5  *  6  */  7   8 $("ol").append("
• "); 
• See more in the jQuery API docs: .append() method.

JavaScript:

 1 /* Syntax */  2   3 Node.appendChild(); 
 1 /**  2  * Example  3  *  4  * For declaring the 'ol' variable, see examples #3, #4, #5  5  */  6   7 ol.appendChild(document.createElement("li")); 

See more in the DOM API docs: .appendChild() method and .createElement() method.

## 18. Prepend a New Child Element

jQuery:

 1 /* Syntax */  2   3 .prepend('html-string'); 
 1 /**  2  * Example  3  *  4  * Prepends an extra list element to the beginning of every ordered list on the page  5  *  6  */  7   8 $("ol").prepend(" • ");  • See more in the jQuery API docs: .prepend() method. JavaScript:  1 /* Syntax */  2   3 Node.insertBefore();   1 /**  2  * Example  3  *  4  * For declaring the 'ol' variable, see examples #3, #4, #5  5  */  6   7 ol.insertBefore(document.createElement("li"), ol.firstChild);  See test code (for both appending and prepending a child node): See more in the DOM API docs: .insertBefore() method, .createElement() method, and firstChild property. ## 19. Remove All Child Nodes jQuery:  1 /* Syntax */  2 .empty();   1 /**  2  * Example  3  *  4  * Removes all child nodes (HTML + text) of each element  5  * that has 'my-class'  6  */  7 $('.my-class').empty(); 

See more in the jQuery API docs: .empty() method.

JavaScript:

 1 /* Syntax */  2 Element.replaceChildren(); 
 1 /**  2  * Example  3  *  4  * Empties the 'item' node of all of its children  5  *  6  * For declaring the 'item' variable, see examples #3, #4, #5  7  */  8  item.replaceChildren(); 

See more in the DOM API docs: .replaceChildren() method.

## 20. Get or Set HTML Content

jQuery:

 1 /* Syntax */  2 3 .html();  4 .html('html-string'); 
 1 /**  2  * Example  3  *  4  * (1) Gets the HTML content of the FIRST element that matches 'my-class'  5  *  6  * (2) Sets/resets the HTML content of EACH element that matches 'my-class'  7  *  8  */  9   10 $('.my-class').html();  11 12 $('.my-class').html('Hello'); 

See more in the jQuery API docs: .html() method.

JavaScript:

 1 /* Syntax */  2 3 Element.innerHTML; 
 1 /**  2  * Example  3  *  4  * For declaring the 'item' variable, see examples #3, #4, #5  5  */  6   7 item.innerHTML; // gets the value  8 item.innerHTML = 'Hello'; // sets the value 

See more in the DOM API docs: .innerHTML property.

## 21. Replace an Element with New HTML Content

jQuery:

 1 /* Syntax */  2 .replaceWith('new-html-content'); 
 1 /**  2  * Example  3  *  4  *  5  * Selects a heading, then replaces the full HTML content,  6  * including the parent element  7  */  8 $('#my-heading').replaceWith(' New Heading ');  See more in the jQuery API docs: .replaceWith() method. JavaScript:  1 /* Syntax */  2 Element.outerHTML = 'new HTML content';   1 /**  2  * Example  3  *  4  * Replaces the full HTML content, including the parent element  5  *  6  */  7 let item = document.querySelector('#my-heading');  8 item.outerHTML = ' New Subheading ';  See more in the DOM API docs: .outerHTML property. ## 22. Get or Set CSS Property jQuery:  1 /* Syntax */  2 3 .css('property-name');  4 .css('property-name', value);   1 /**  2  * Example  3  *  4  * (1) Gets the 'color' value of the FIRST element  5  * that has 'my-class'  6  *  7  * (2) Sets the 'color' value to 'white' for EVERY element that has  8  * 'my-class'  9  */  10   11 $('.my-class').css('color');  12 13 $('.my-class').css('color', 'white');  See more in the jQuery API docs: .css() method. JavaScript:  1 /* Syntax */  2 3 ElementCSSInlineStyle.style.{propertyName};   1 /**  2  * Example  3  *  4  * For declaring the 'item' variable, see examples #3, #4, #5  5  */  6   7 item.style.color; // getting value  8 9 item.style.color = 'white'; // setting value  See more in the DOM API docs: .style property and CSS Properties Reference (for the JavaScript names of CSS properties). ## 23. Get or Set Text Content of an Element and All of Its Descendants jQuery:  1 /* Syntax */  2   3 .text();  4 .text('text');   1 /**  2  * Example  3  *  4  * (1) Gets the text content of the FIRST element (and all of its descendants) that matches 'my-class'  5  *  6  * (2) Sets/resets the text content of EACH element that matches 'my-class'  7  *  8  */  9   10 $('.my-class').text();  11   12 $('.my-class').text('Hello');  See more in the jQuery API docs: .text() method. JavaScript:  1 /* Syntax */  2   3 Element.textContent;   1 /**  2  * Example  3  *  4  * For declaring the 'item' variable, see examples #3, #4, #5  5  */  6   7 item.textContent; // gets the value  8 item.textContent = 'Hello'; // sets the value  See more in the DOM API docs: .textContent property. ## 24. Get or Set Input Values jQuery:  1 /* Syntax */  2   3 .val();  4 .val(val);   1 /**  2  * Example  3  *  4  * (1) Gets the value of the input with the 'name' name  5  *  6  * (2) Sets/resets the value of the input with the 'name' name  7  *  8  */  9   10 $('input[name=name]').val();  11   12 $('input[name=name]').val('Marilyn Monroe');  See more in the jQuery API docs: .val() method. JavaScript:  1 /* Syntax */  2   3 HTMLInputElement.value;   1 /**  2  * Example  3  *  4  * For declaring the 'input' variable, see examples #3, #4, #5  5  */  6   7 input.value; // gets the value  8 input.value = 'Marilyn Monroe'; // sets the value  See more in the DOM API docs: .value property (in the list of ''Properties that apply to any type of input element that is not hidden"). ## 25. Hide Element jQuery:  1 /* Syntax */  2 3 .hide();   1 /**  2  * Example  3  *  4  * Hides all elements with 'my-class'  5  */  6   7 $('.my-class').hide(); 

See more in the jQuery API docs: .hide() method.

JavaScript:

 1 /* Syntax */  2 3 ElementCSSInlineStyle.style.display = 'none'; 
 1 /**  2  * Example  3  *  4  * For declaring the 'item' variable, see examples #3, #4, #5  5  */  6   7 item.style.display = 'none'; 

See more in the DOM API docs: .style property.

## 26. Show Element

jQuery:

 1 /* Syntax */  2 3 .show(); 
 1 /**  2  * Example  3  *  4  * Displays all elements with 'my-class'  5  */  6 7 $('.my-class').show()  See more in the jQuery API docs: .show() method. JavaScript:  1 /* Syntax */  2 3 ElementCSSInlineStyle.style.display = '';   1 /**  2  * Example  3  *  4  * For declaring the 'item' variable, see examples #3, #4, #5  5  */  6 7 item.style.display = ''; // resets default  8 item.style.display = 'block'; // sets display as block  9 item.style.display = 'flex'; // sets display as flex  See more in the DOM API docs: .style property. ## 27. Add Event Listener jQuery:  1 /* Syntax */  2 3 .on();   1 /**  2  * Example  3  *  4  * Adds or removes the 'active' class to/from all elements with  5  * '.submenu' when #toggle is clicked  6  */  7   8 $('#toggle').on('click', function(){  9  $('.submenu').toggleClass('active');  10 });  See more in the jQuery API docs: .on() method JavaScript:  1 /* Syntax */  2 3 EventTarget.addEventListener('event', functionName);   1 /**  2  * Example  3  *  4  * The code below only selects the FIRST element with the  5  * 'submenu' class.  6  *  7  * To select all submenus, use the 'for' loop in Example #3 and #4  8  */  9 10 let toggle = document.querySelector("#toggle");  11 let submenu = document.querySelector(".submenu");  12 13 toggle.addEventListener('click', function() {  14  submenu.classList.toggle('active');  15 });  See test code: See more in the DOM API docs: .addEventListener() method and DOM Event Reference. ## 28. Remove Event Listener jQuery:  1 /* Syntax */  2   3 .off();   1 /**  2  * Example  3  *  4  * Removes the listed event handler when #toggle is clicked  5  */  6   7 $('#toggle').off('click', function(){  8  $('.submenu').toggleClass('active');  9 });  See more in the jQuery API docs: .off() method. JavaScript:  1 /* Syntax */  2   3 EventTarget.removeEventListener('event', functionName);   1 /**  2  * Example  3  *  4  * The code below only selects the FIRST element with the  5  * 'submenu' class.  6  *  7  * To select all submenus, use the 'for' loop in Example #3 and #4  8  */  9   10 let toggle = document.querySelector("#toggle");  11 let submenu = document.querySelector(".submenu");  12   13 toggle.removeEventListener('click', function() {  14  submenu.classList.toggle('active');  15 });  See more in the DOM API docs: .removeEventListener() method and DOM Event Reference. ## 29. Execute a Function When the DOM Is Ready jQuery:  1 /**  2  * Syntax  3  *  4  * This is the jQuery 3 version of$(document).ready(function(){});  5  * which is now deprecated.  6  */  7 $(function() {  8  // event handler  9 });  10  1 /**  2  * Example  3  *  4  * Event: The DOM (document) is fully loaded  5  *  6  * Action triggered by this event: Message in the console  7  */  8 $(function() {  9  console.log('The DOM is ready!');  10 }); 

See more in the jQuery API docs: .ready() method.

JavaScript:

 1 /* Syntax version 01 */  2 if(document.readyState === 'complete' || (document.readyState !== 'loading')) {  3  // event handler  4 } else {  5  document.addEventListener('DOMContentLoaded', function() {  6  // event handler  7  });  8 }  9 10 /**  11  * Syntax version 02 - you can store the event handler  12  * in a custom callback function  13  */  14 let handler() = function(){  15  // event handler  16 };  17 18 if(document.readyState === 'complete' || (document.readyState !== 'loading')) {  19  handler()  20 } else {  21  document.addEventListener('DOMContentLoaded', handler);  22 } 
 1 /** Example  2  *  3  * When the document is fully loaded, a message is logged to the console.  4  */  5 6 // version 01  7 if(document.readyState === 'complete' || (document.readyState !== 'loading')) {  8  console.log('The DOM is ready!');  9 } else {  10  document.addEventListener('DOMContentLoaded', function() {  11  console.log('The DOM is ready!');  12  });  13 }  14 15 // version 02  16 // (with a custom 'logMessage' function that holds the event handler)  17 let logMessage = function(){  18  console.log('The DOM is ready!');  19 };  20 21 if(document.readyState === 'complete' || (document.readyState !== 'loading')) {  22  logMessage();  23 } else {  24  document.addEventListener('DOMContentLoaded', logMessage);  25 } 

## 30. Loop Through an Object, Array, or Collection

jQuery:

 1 /* Syntax */  2 .each(); 
 1 /**  2  * Example  3  *  4  * Finds each paragraph and turns them yellow  5  */  6 $('p').each(function() {  7 $(this).css('color', '#fbdf00');  8 }); 

See more in the jQuery API docs: .each() method.

JavaScript:

 1 /* Syntax */  2 NodeList.prototype.forEach(); // we'll use this in the example below  3 Array.prototype.forEach();  4 Map.prototype.forEach();  5 Set.prototype.forEach(); 
 1 /**  2  * Example  3  *  4  * v1 turns red all paragraphs on the page  5  *  6  * v2 turns green all list items on the page  7  */  8   9 // v1 (inline callback function syntax)  10 let itemsV1 = document.querySelectorAll('p');  11 itemsV1.forEach(  12  function(item) {  13  item.style.color = 'white';  14  }  15 );  16 17 // v2 (arrow function syntax)  18 let itemsV2 = document.querySelectorAll('li');  19 itemsV2.forEach(item => item.style.color = '#fbdf00'); 

See test demo:

See more in the DOM API docs: .foreach() methods of the NodeList, Array, Map, and Set interfaces.

## Next Steps

This jQuery to JavaScript cheat sheet includes the most important functionalities that you’ll need for building a website. By familiarizing yourself with these properties and methods, you can make the jump from jQuery to JavaScript. However, you’ll likely need to know more if you want to build more complex functionality.

There are two other resources I’d recommend to find the jQuery to JavaScript equivalent you need: the You Might Not Need jQuery website and Jack O’Connor’s jQuery-to-JavaScript repo on GitHub.

Even though I used the classic for loop to loop through querySelectorAll() matches, modern JavaScript provides you with other options, too — Chris Coyier shows some of them in this article about JavaScript loops.

Experimenting and creating your own code snippets can also help a lot if you want to confidently use native JavaScript and the DOM API in your everyday workflow.