Remove all HTML tags using javascript
by MItul Patel
HTML
<table>
<tr class='record'>
<td class='sh_title'> Title</td>
<td class='sh_post'> <div> this < is & the content </div> new <br /> one</td>
<td class='postid'> 1</td>
<td><a class='editButton' href="">Edit</a> </td>
</tr>
<tr class='record'>
<td class='sh_title'> Title 2</td>
<td class='sh_post'> this is the content 2 <br/> new one <br /> Line 2</td>
<td class='postid'> 2</td>
<td><a class='editButton' href="">Edit</a> </td>
</tr>
</table>
<table>
<tr>><td>Post Title</td><td><textarea id='Title'></textarea></td></tr>
<tr>><td>Desc</td><td><textarea id='Post'></textarea></td></tr>
<tr>><td>Id</td><td><textarea id='PostId'></textarea></td></tr>
</table>
JavaScript
$(document).ready(function () {
var record;
//set up the button styling and click functionality
$('.editButton').click(function () {
//set which record we're editing so we can update it later
record = $(this).parents('.record');
//populate the editing form within the dialog
$('#Title').val(cleanText(record.find('.sh_title').html()));
$('#Post').val(cleanText(record.find('.sh_post').html()));
$('#PostId').val(cleanText(record.find('.postid').html()));
//show the dialog
// $("#dialogContent").dialog("open");
return false;
});
//set up the dialog box.
$("#dialogContent").dialog({
autoOpen: false,
modal: true,
buttons: {
OK: function () {
record.find('.sh_title').text($('#Title').val());
record.find('.sh_post').text($('#Post').val());
$(this).dialog("close");
},
Cancel: function () {
$(this).dialog("close");
}
}
});
});
function cleanText(text){debugger;
text = text.replace(/<\/p>/mg, "<br></p>");
text = text.replace(/<\/div>/mg, "<br></div>");
text = text.replace(/<br\s*\/?>/mg, "\n");
text = text.replace(/<p/mg, "<br><p");
text = text.replace(/<br\s*\/?>/mg, "\n");
text = text.replace(/ /mg, " ");
text = text.trim();
text = htmlDecode(text);
var regex = /(<([^>]+)>)/ig;
return text.replace(regex, "");
}
function htmlDecode(input){
var e = document.createElement('div');
e.innerHTML = input;
return e.innerText;
}