Home > Net >  jQuery/Vanilla JS - parse string to HTML and get it own height
jQuery/Vanilla JS - parse string to HTML and get it own height

Time:11-05

I need to parse many string values (which are probably HTML) to HTML and then, get it own height (as HTML node). I don't want to create real node inside my real HTML, probably there is a way to create fake one and get it height?

Example:

const arrOfHtml = ["<div>Test</div>", "<div><h2>Test2></h2></div><div><p>Im not ok</p></div>"];
arrOfHtml.forEach(el => {
  console.log($(el).height())
})

CodePudding user response:

Pointy makes a great point in their comment.

If you want to get the height of an HTML node, you have to render it. You don't really have a choice, elements have to be added to the DOM. I've found that performance isn't usually an issue unless you have hundreds of elements.

  • Related