2

I'm loading text from database but I'd like to remove html link code from it with JavaScript.

So lets say the textarea right now displays:

<a rel="nofollow" href="http://stackoverflow.com//questions/ask">http://stackoverflow.com//questions/ask</a> - good page 

and I want it to display:

http://stackoverflow.com//questions/ask - good page

Is there something lightweight I could use that would work for multiple links in the same textarea?

3 Answers 3

8

Inspired by this answer, use the browser's HTML parsing abilities to get this done right.

function strip(html)
{
   var tmp = document.createElement("DIV");
   tmp.innerHTML = html;
   return tmp.textContent||tmp.innerText;
}
jQuery('#textareaid').text(function(index, text){
 return strip(text);
});

Here's the JSFiddle of it working: http://jsfiddle.net/Au95R/1/

(Edited to use cleaner JS)

Sign up to request clarification or add additional context in comments.

1 Comment

Bear in mind that in IE8 innerText will not respect newlines \r\n, and as a result it can seriously mess with the content of a textarea.
1

You could use strip_tag() like in PHP: http://phpjs.org/functions/strip_tags:535

textareacontent = strip_tags(textareacontent, "<b><i>"); // remove all HTML except <b> and <i>.

Comments

1

you can do this using regular expressions. here is a question on stack overflow itself and the answer explains it well

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.