Aaron
Aaron

Reputation: 1583

How to close html tags in JavaScript?

I'm taking html content out of my database and displaying it on a page.

Unfortunately these pages often have unclosed html tags and cause problems later when the page is rendered.

I was wondering if there is a JavaScript implementation of something like tidy or htmlpurifier.

Basically some software which can preferably close html tags in a string.

Edit: I'm not in a browser environment (node.js)

Upvotes: 2

Views: 2244

Answers (3)

Juicy Scripter
Juicy Scripter

Reputation: 25918

As you tagged your question with :

npm install tidy

Upvotes: 4

Niet the Dark Absol
Niet the Dark Absol

Reputation: 324640

Something like this could work:

function tidy(htmldata) {
    var d = document.createElement('div');
    d.innerHTML = htmldata;
    return d.innerHTML;
}

Upvotes: 3

Related Questions