js parsehtml code example

Example 1: html parser javascript

var parser = new DOMParser();
var htmlDoc = parser.parseFromString(txt, 'text/html');
htmlDoc.getElementById("someID");

Example 2: jquery parse html

// use for nodejs (without window)
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
const { window } = new JSDOM();
const { document } = (new JSDOM('')).window;
global.document = document;

const $ = jQuery = require('jquery')(window);

// and here is your jQuery object of your html string
const html = $("<div/>").append( html );

Example 3: jquery parse html

const $ = jQuery = require('jquery');

// and here is your jQuery object of your html string
const html = $("<div/>").append( html );