Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse HTML as a plain text via JavaScript

I am making some ajax request that returns the whole HTML page as the response. I need to grab some data from that page, in particular, value of specific <input>.

What is the best way to do that?

My ideas:

  • Find where the <body> tag ends and </body> starts, grab all stuff inside to string, and put via innerHTML to some container.
  • Self-made parser: find the character position of the id I need, convert response string to an array, set position of reading equals position of id character, shift to where " character starts, read to buffer until new " come.

It would be perfect if there is a framework that uses classic DOM syntax to do that, like:

htmlString.getElementById("someid").value
like image 759
Artur Avatar asked Aug 28 '26 15:08

Artur


1 Answers

A pretty elegant solution is to use DOMParser.

const parser = new DOMParser()
const virtualDoc = parser.parseFromString(htmlString, 'text/html')

Then, treat virtualDoc like you'd treat any DOM-element,

virtualDoc.getElementById('someid').value
like image 79
eds1999 Avatar answered Sep 01 '26 03:09

eds1999