How to convert jQuery to JavaScript ?
JavaScript is an object orient programming language designed to make web development easier and more attractive. In most cases, JavaScript is used to create responsive, interactive elements for web pages, enhancing the user experience.
jQuery is an open-source JavaScript library that simplifies the interactions between an HTML/CSS document, or more precisely the Document Object Model (DOM), and JavaScript.
Selection: In jQuery, to select any element, we simply use the $() sign, but in JavaScript, to select any element, we can use querySelector() or querySelectorAll().
- Program:
// jQuery to select all instances// of class "select"$(".select");// JavaScript to select only the// first instance of class "select"document.querySelector(".select");// To select all the instances// of class "select"document.querySelectorAll(".select");chevron_rightfilter_none
Some other examples of selectors:
To select the entire html:
- In jQuery:
$("html") - In JavaScript:
document.querySelector(selector)
To select the entire html body:
- In jQuery:
$("body") - In JavaScript:
document.body
Class manipulation:
- Program:
// To add a class "class-name" to a <p> tag// jQuery:$p.addClass(class-name)// JavaScript:p.classList.add(class-name)chevron_rightfilter_none
Below some other examples of manipulation:
To add a class to an html element:
- In jQuery:
$element.addClass(class-name)
- In JavaScript:
element.classList.add(class-name)
To remove a class to an html element:
- In jQuery:
$element.removeClass(class-name)
- In JavaScript:
element.classList.remove(class-name)
To toggle a class to an html element:
- In jQuery:
$element.toggleClass(class-name)
- In JavaScript:
element.classList.toggle(class-name)
To check whether an html element contains a class:
- In jQuery:
$element.hasClass(class-name)
- In JavaScript:
element.classList.has(class-name)
Event Listeners
- Program:
// To add an event on button click// jQuery:/* handle click event */$(".button").click(function(event) {});// JavaScript:/* handle click event */document.querySelector(".button").addEventListener("click", (event) => {});chevron_rightfilter_none
CSS Styling:
- Program:
// To give a margin of 10px to all the div// jQuery:$div.css({ margin:"10px"})// JavaScript:div.style.margin="10px"chevron_rightfilter_none
Recommended Posts:
- How to convert a currency string to a double value with jQuery or Javascript?
- How to Convert JS Object to JSON String in JQuery/Javascript?
- How to convert JSON data to a html table using JavaScript/jQuery ?
- How to compare two JavaScript array objects using jQuery/JavaScript ?
- How to convert list of elements in an array using jQuery ?
- How to Convert HTML Table into Excel Spreadsheet using jQuery ?
- How to convert first letter of a string to upper case using jQuery ?
- When to use Vanilla JavaScript vs jQuery ?
- How to flatten array with the JavaScript/jQuery ?
- How to get form data using JavaScript/jQuery?
- Scroll to the top of the page using JavaScript/jQuery
- How to check two elements are same using jQuery/JavaScript ?
- Implode an array with jQuery/JavaScript
- How to use JavaScript variables in jQuery selectors ?
- How to get the ID of the clicked button using JavaScript / jQuery ?
- How to convert NaN to 0 using JavaScript ?
- How to check caps lock is on/off using JavaScript / jQuery ?
- How to add content in <head> section using jQuery/JavaScript?
- How to disable HTML links using JavaScript / jQuery ?
- How to change the src attribute of an img element in JavaScript / jQuery ?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


