JS Problems geeksforgeeks Exercises and Solutions for Beginners

How to Use $ in JavaScript without JQuery 📌[All Method]️

The blog is about How to Use $ in JavaScript without JQuery & provides a lot of information to the novice user and the more seasoned user. By the end of this guide, you will know how to handle these types of problems.
Question: What is the best solution for this problem? Answer: This blog code can help you solve errors How to Use $ in JavaScript without JQuery. Question: What is causing this error and what can be done to fix it? Answer: Check out this blog for a solution to your problem.

I remember the days of jQuery, when we could use $ to target anything in the DOM.

In JavaScript, I do find myself using document.querySelector() quite often, which functions very similarly to jQuery’s $.

Luckily, we can bind $ to document.querySelector().

const $ = document.querySelector.bind(document);
const $$ = document.querySelectorAll.bind(document);

Let’s see what we can do with $, or document.querySelector(), which only selects first matching element.

$('.text').style.color = "#fff";
$('#text').innerHTML = "Hello!";

What about $$, or document.querySelectorAll(), which returns a NodeList of all matching elements?

$$('img').forEach(img => console.log(img.src));

This functionality is actually already available in Chrome’s DevTools without the need to manually bind $ or $$ to document.querySelector() or document.querySelectorAll(), respectively.


Now you learned, How you can use & How to Use $ in JavaScript without JQuery.
If you need assistance at any stage, please feel free to contact me.

Related Articles

Leave a Reply

Your email address will not be published.

Back to top button