0% found this document useful (0 votes)
103 views

How To Use Jquery With Node - Js - GeeksforGeeks

Uploaded by

luca pilotti
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
103 views

How To Use Jquery With Node - Js - GeeksforGeeks

Uploaded by

luca pilotti
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Tutorials Student Jobs GeekWeek Sign In

Related Articles Save for later

How to use jQuery with Node.js ?


Last Updated : 15 Mar, 2021

jQuer y is a JavaScript librar y, which provides us with the same functionalit y

as vanilla JavaScript but in fewer lines of code. The need for jQuer y is

reduced as doing things became much simpler in vanilla JavaScript with

updates. Although it s popularit y is decreasing, still around 76% of the

project s use jQuer y.

Our Aim is to use jQuer y with Node.js : We can use jQuer y in Node.js using

the jquer y module. 

Note : Use the ‘jquer y ’ module not the ‘jQuer y ’ module as the latter is

deprecated.

WHAT'S NEW
Getting jQuer y to work in Node.js :

Step 1 : Making the package.json file. Use the following command to

create the package.json file, which keeps track of the modules and
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
dependencies.  Policy ▲ Geek Week - 19th to Got
25thIt !
October

View Details

Data Structures using C++ Live


Classes
View Details

DSA Self Paced Course

View Details

npm init -y

The ‘-y ’ tag makes yes the default answer for all the questions asked

while creating the package.json file.

Step 2: Installing the jquer y module. Use the following command to install

the jquer y module.

npm install jquery

Step 3 :Installing the jsdom module. Since jQuer y is a frontend JavaScript

Librar y, it needs to have a window with a document to work in the

backend. ‘jsdom’ is a librar y that is used to parse and interact with the

HTML. It is not exactly a web browser but mimic s one. Use the following

command to install the jsdom module.

npm install jsdom

Step 4 :Impor ting the jsdom module. Use the require method to impor t the

jsdom module.

const jsdom = require('jsdom')


MOST POPULAR IN JQUERY
Step 5: Creating a new window. We create a window with a document by

creating a JSDOM object, with the HTML code as the parameter. Following Form validation using jQuery
code is used to create a window with a document :- 

Scroll to the top of the page using


const dom = new jsdom.JSDOM("") JavaScript/jQuery

Step 6: Impor ting jQuer y and providing it with a window. Once the window
How to check whether a checkbox is checked in
with a document is created, we can use the jquer y module by providing it
jQuery?
with the window we created. The following code is used to impor t the

jquer y module.
How to Dynamically Add/Remove Table Rows
using jQuery ?
const jquery = require('jquery')(dom.window)
How to get the ID of the clicked button using
JavaScript / jQuery ?
That ’s it we have successfully loaded jquer y into our Node.js application.

Ad
Example : To get a better understanding of how it work s, please go through

the below example :- 

Javascript

// Importing the jsdom module


const jsdom = require("jsdom"); Secrets Management

// Creating a window with a document Doppler Open


const dom = new jsdom.JSDOM(`<!DOCTYPE html>
<body>
<h1 class="heading">
GeeksforGeeks
</h1>
</body>
`);

// Importing the jquery and providing it


// with the window
const jquery = require("jquery")(dom.window);

// Appending a paragraph tag to the body


jquery("body").append("<p>Is a cool Website</p>");

// Getting the content of the body


const content = dom.window.document.querySelector("body");

// Printing the content of the heading and paragraph


console.log(content.textContent);

MOST VISITED IN NODE.JS


Output :
Node.js fs.readFileSync() Method

GeeksforGeeks Difference between Fetch and Axios.js for making


Is a cool website http requests

Difference between promise and async await in


Node.js

Like 0
Node.js fs.readFile() Method

Next How to install the previous version of node.js and


npm ?
How do you run JavaScript
script through the Terminal?

RECOMMENDED ARTICLES Page : 1 2 3

How to use autocomplete How to use complex HTML


01 05
search in jQuery ? with twitter Bootstrap tooltip
14, Apr 21
using jQuery ?
23, Apr 20

How to use jQuery to Search


02
and Replace HTML elements? How to use .on and .hover in
06
05, Jun 20
jQuery ?
22, Apr 20

When to use Vanilla How to use the Magnific


03
JavaScript vs jQuery ? 07
Popup jQuery plugin?
15, Nov 19
14, Jul 20

How to use Explode effect in How to use jQuery touch


04 08
jQuery ? events plugin for mobiles ?
18, Nov 19 14, Oct 20

Ar ticle Contributed By :

coder_srinivas
@coder_srinivas

Vote for di culty

Easy Normal Medium Hard Expert

Article Tags : jQuery-Questions, NodeJS-Questions, Picked,


Technical Scripter 2020, JQuery, Node.js, Technical Scripter,
Web Technologies

Improve Article Report Issue

Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.

Load Comments

Company Learn Web Development Contribute


5th Floor, A-118, About Us Algorithms Web Tutorials Write an Article
Sector-136, Noida, Uttar Pradesh - 201305
[email protected] Careers Data Structures HTML Write Interview Experience
Privacy Policy Languages CSS Internships
Contact Us CS Subjects JavaScript Videos
Copyright Policy Video Tutorials Bootstrap

@geeksforgeeks , Some rights reserved

You might also like