You can use jQuery's .text()
function which will remove HTML tags:
var text_only = $('.snippet').text();
Here is a demonstration: http://jsfiddle.net/meZjw/
Docs for .text()
: http://api.jquery.com/text
UPDATE
Sime Vidas has a good point, you can iterate through the different .snippet
elements changing their HTML structure one at a time:
$.each($('.snippet'), function (index, obj) {
var $this = $(this);
$this.html($this.text());
});
Here is a demo using $.each()
: http://jsfiddle.net/meZjw/1/
UPDATE
Aepheus has a good point, I don't know if this is what is being asked but you can make a function that will escape HTML entities like in other languages:
function htmlEntities(str) {
return String(str).replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>').replace(/"/g, '"');
}
Here is demo: http://jsfiddle.net/meZjw/2/
UPDATE
You can also use .text()
and .html()
in the opposite order as my above example, to the effect of showing the HTML of an element as plain-text:
$.each($('.snippet'), function (index, obj) {
var $this = $(this);
$this.text($this.html());
});
Here is a demo: http://jsfiddle.net/meZjw/31/
.snippet
elements as text (with the tag names)? I'm a little confused by your wording. – Lurette