Essential Cheat Sheet: Convert jQuery to JavaScript

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.

To help you with the move, here’s a jQuery to JavaScript cheat sheet that includes the JavaScript equivalents to the most frequently used jQuery functionality.

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:

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:

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.

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

JavaScript:

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

2. Select First Element

jQuery:

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

JavaScript:

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

3. Find Elements

jQuery:

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

JavaScript:

See code demo:

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

4. Select Children

jQuery:

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

JavaScript:

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:

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

JavaScript:

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

6. Select Siblings

jQuery:

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

JavaScript:

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:

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

JavaScript:

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

8. Select Previous Sibling

jQuery:

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

JavaScript:

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

9. Find the Closest Matching Element

jQuery:

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

JavaScript:

See test demo:

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

10. Add Class

jQuery:

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

JavaScript:

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

11. Remove Class

jQuery:

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

JavaScript:

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

12. Toggle Class

jQuery:

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

JavaScript:

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

13. Has Class

jQuery:

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

JavaScript:

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

14. Get Attribute

jQuery:

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

JavaScript:

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

15. Set Attribute

jQuery:

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:

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

16. Remove Attribute

jQuery:

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

JavaScript:

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

17. Append a New Child Element

 jQuery:

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

JavaScript:

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

18. Prepend a New Child Element

jQuery:

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

JavaScript:

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:

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

JavaScript:

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

20. Get or Set HTML Content

jQuery:

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

JavaScript:

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

21. Replace an Element with New HTML Content

jQuery:

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

JavaScript:

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

22. Get or Set CSS Property

jQuery:

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

JavaScript:

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:

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

JavaScript:

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

24. Get or Set Input Values

jQuery:

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

JavaScript:

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:

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

JavaScript:

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

26. Show Element

jQuery:

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

JavaScript:

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

27. Add Event Listener

jQuery:

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

JavaScript:

See test code:

See more in the DOM API docs: .addEventListener() method and DOM Event Reference.

28. Remove Event Listener

jQuery:

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

JavaScript:

See more in the DOM API docs: .removeEventListener() method and DOM Event Reference.

29. Execute a Function When the DOM Is Ready

jQuery:

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

JavaScript:

See more in the DOM API docs: readyState property, addEventListener() method.

30. Loop Through an Object, Array, or Collection

jQuery:

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

JavaScript:

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.

Learn More JavaScript with Tuts+

If you’re just starting out with JavaScript these Tuts+ courses will put you in the right direction:


This content originally appeared on Envato Tuts+ Tutorials and was authored by Anna Monus

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.

To help you with the move, here’s a jQuery to JavaScript cheat sheet that includes the JavaScript equivalents to the most frequently used jQuery functionality.

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:

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:

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.

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

JavaScript:

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

2. Select First Element

jQuery:

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

JavaScript:

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

3. Find Elements

jQuery:

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

JavaScript:

See code demo:

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

4. Select Children

jQuery:

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

JavaScript:

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:

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

JavaScript:

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

6. Select Siblings

jQuery:

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

JavaScript:

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:

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

JavaScript:

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

8. Select Previous Sibling

jQuery:

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

JavaScript:

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

9. Find the Closest Matching Element

jQuery:

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

JavaScript:

See test demo:

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

10. Add Class

jQuery:

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

JavaScript:

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

11. Remove Class

jQuery:

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

JavaScript:

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

12. Toggle Class

jQuery:

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

JavaScript:

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

13. Has Class

jQuery:

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

JavaScript:

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

14. Get Attribute

jQuery:

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

JavaScript:

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

15. Set Attribute

jQuery:

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:

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

16. Remove Attribute

jQuery:

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

JavaScript:

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

17. Append a New Child Element

 jQuery:

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

JavaScript:

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

18. Prepend a New Child Element

jQuery:

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

JavaScript:

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:

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

JavaScript:

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

20. Get or Set HTML Content

jQuery:

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

JavaScript:

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

21. Replace an Element with New HTML Content

jQuery:

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

JavaScript:

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

22. Get or Set CSS Property

jQuery:

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

JavaScript:

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:

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

JavaScript:

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

24. Get or Set Input Values

jQuery:

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

JavaScript:

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:

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

JavaScript:

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

26. Show Element

jQuery:

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

JavaScript:

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

27. Add Event Listener

jQuery:

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

JavaScript:

See test code:

See more in the DOM API docs: .addEventListener() method and DOM Event Reference.

28. Remove Event Listener

jQuery:

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

JavaScript:

See more in the DOM API docs: .removeEventListener() method and DOM Event Reference.

29. Execute a Function When the DOM Is Ready

jQuery:

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

JavaScript:

See more in the DOM API docs: readyState property, addEventListener() method.

30. Loop Through an Object, Array, or Collection

jQuery:

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

JavaScript:

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.

Learn More JavaScript with Tuts+

If you’re just starting out with JavaScript these Tuts+ courses will put you in the right direction:


This content originally appeared on Envato Tuts+ Tutorials and was authored by Anna Monus


Print Share Comment Cite Upload Translate Updates
APA

Anna Monus | Sciencx (2021-04-07T09:08:59+00:00) Essential Cheat Sheet: Convert jQuery to JavaScript. Retrieved from https://www.scien.cx/2021/04/07/essential-cheat-sheet-convert-jquery-to-javascript/

MLA
" » Essential Cheat Sheet: Convert jQuery to JavaScript." Anna Monus | Sciencx - Wednesday April 7, 2021, https://www.scien.cx/2021/04/07/essential-cheat-sheet-convert-jquery-to-javascript/
HARVARD
Anna Monus | Sciencx Wednesday April 7, 2021 » Essential Cheat Sheet: Convert jQuery to JavaScript., viewed ,<https://www.scien.cx/2021/04/07/essential-cheat-sheet-convert-jquery-to-javascript/>
VANCOUVER
Anna Monus | Sciencx - » Essential Cheat Sheet: Convert jQuery to JavaScript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/04/07/essential-cheat-sheet-convert-jquery-to-javascript/
CHICAGO
" » Essential Cheat Sheet: Convert jQuery to JavaScript." Anna Monus | Sciencx - Accessed . https://www.scien.cx/2021/04/07/essential-cheat-sheet-convert-jquery-to-javascript/
IEEE
" » Essential Cheat Sheet: Convert jQuery to JavaScript." Anna Monus | Sciencx [Online]. Available: https://www.scien.cx/2021/04/07/essential-cheat-sheet-convert-jquery-to-javascript/. [Accessed: ]
rf:citation
» Essential Cheat Sheet: Convert jQuery to JavaScript | Anna Monus | Sciencx | https://www.scien.cx/2021/04/07/essential-cheat-sheet-convert-jquery-to-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.