-1

How to use jquery in node js. Please Help me I'm using this code

 const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const { window } = new JSDOM('<!DOCTYPE html>');
const $ = require('jquery')(window);
var connection = require('./database');
connection.query(
 "SELECT * FROM cr_state",
  function(error, results, fields) {
    if (error) throw error;
    $('#state').html('<option value="1">test</option>')
  }
);

But this code is not working.How to use jquery?

1 Answers1

1

By Default Nodejs is not designed to read DOM

$('#state').html(html); // jquery virtual DOM

if you wanna read DOM using node then you have to install DOM parser.

For more info: Why doesn't Node.js have a native DOM?

and to install DOM parser: How do I parse a HTML page with Node.js

Vinesh Goyal
  • 607
  • 5
  • 8
  • I'm using this code for read DOM var jsdom = require("jsdom"); const { JSDOM } = jsdom; const { window } = new JSDOM(); const { document } = (new JSDOM('')).window; global.document = document; var $ = jQuery = require('jquery')(window); – shivani chourasiya Dec 13 '18 at 07:06