Strip HTML Tags from String

Remove all HTML tags from a string. If a user submits comments or other content, HTML tags need to be stripped in order control formatting. This function removes any potentially harmful code or user formatted code so your input is clean.

textcomment = strip_tags(textcomment);

function strip_tags(i) {
	var tmp = document.createElement("DIV");
	tmp.innerHTML = i;
	return tmp.textContent||tmp.innerText;
}

Example:

javascript

textcomment = "Hello, my name is <b onclick='alert(\"hi\")>George</b> and I will insert some javascript into my text.";
textcomment = strip_tags(textcomment);

Produces the result: 

textcomment has this string: Hello, my name is George and I will insert some javascript into my text.