I have a javascript string that has HTML code. I display it and I have attached a read more/less toggler based on the number of words. The problem is, when I shrink the HTML code, it might have open tags, for suppose
<p>A computer is a general purpose device that can be <b>programmed</b> to carry out a finite set of arithmetic or logical operations</p>
when shrinked becomes
<p>A computer is a general purpose device that can be <b>programmed...more</p>
Because of unclosed bold tag, the following data becomes bold.
I want a javascript solution to close the unclosed tags in a string. Any kind of help is highly appreciated. Thank you all in advance.
Use this code
function fixHtml(html){
var div = document.createElement('div');
div.innerHTML=html
return (div.innerHTML);
}
using
var fixed = fixHtml("<b>some text")
will return
<b>some text</b>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With