0

I am trying to read div contents of an html file with node js on server without using the browser. I would like to be able to use jQuery operations as well.

More specifically, something like content.find("div.t").

Is there a native way of doing this?

I tried to use fs.readfilesync to read the html file on the server and to get the content, but this gives me a string, not html that I can make jQuery operations on.

Thanks.

Irshad
  • 3,071
  • 5
  • 30
  • 51
efe
  • 3
  • 3

1 Answers1

-3

Should work in the Simplest way :

var $ = require('jquery');
var htmlFileString = <valid html of some sort>;

var rootElement = $(htmlFileString);
rootElement.find('div.t'); 
SirDemon
  • 1,758
  • 15
  • 24
  • I tried this as the following: let stringContent = fs.readFileSync(path.join(__dirname, '../pdf/LabReport1.html'), 'utf8'); var rootElement = $(stringContent); I got the error: 'throw new Error( "jQuery requires a window with a document" );' – efe Jul 30 '15 at 14:45
  • Then I'll refer you to the possible duplicate mentioned on your questions comments. Seems to have worked out there with jsdom. – SirDemon Jul 31 '15 at 06:33
  • That's right. I did it with cheerio. – efe Jul 31 '15 at 12:16