JS - Strip variable of html tags and replace with line breaks
I've been going round in circles for ages with this. I'm trying to create
a function in javascript (or jQuery) that will take a variable filled with
html and output a plain text version. The function needs to strip the html
tags and insert line breaks at the end of heading and paragraph tags.
The html variable would be something like:
var html = '<h1>This is a heading</h1><p>This is a paragraph</p><p>This is
another paragraph</p>'
This is really close: http://jsfiddle.net/wv49v/, but it doesn't take a
variable, only a DOM object (I think!). I.e. this works:
document.getElementById("text").value =
getInnerText(document.getElementById("content"));
and this doesn't:
document.getElementById("text").value = getInnerText(html);
If there is a better solution to this I'm open to suggestions!
No comments:
Post a Comment