I have set up a little web application where you add post-its to a page. It\'s just a project to help better learn JavaScript/jQuery and it seemed like an appropriate projec
$("textarea.text_input").on('keyup', function() {
this.value = this.value.replace(/<(.|\n)*?>/g, '');
});
FIDDLE
No one successfully answered this question in accordance with the OP's request, in that the inner tags would be completely removed from the input, not just the tags themselves; so, here's an answer that will do precisely that (Fiddle):
var input = "This is <b>not</b>a successful test";
var output = $("<div/>").html(input).children().remove().end().text()
var textAreaValue = "text <br /> more text";
var strippedText = $("<div/>").html(textAreaValue).text();
And here's a Fiddle.
function stripHTML(text){
var regex = /(<([^>]+)>)/ig;
return text.replace(regex, "");
}
// USE: var str = stripHTML('<b>test</b>');
Demo: http://jsfiddle.net/pisabev/eJzfw/3/
You could use the jquery .text function, though it will rather escape the html than strip it:
$(".myTxtBox").text("<b>this text will be displayed, even the b tag.");
You have three issues: One is that the method is not returning properly (that's the fault of whoever wrote it). Next is that you are defining the method within $(document).ready()
, so it is not able to be found (at least in Chrome). Third, you are not calling it the right way (onSubmit
will never be used here).
I've used @Joao's method (so you should upvote his answer) and used this method, moved outside of the ready()
function:
function stripHTML(str){
var strippedText = $("<div/>").html(str).text();
return strippedText;
}
And also called it here:
var post = stripHTML($(".text_input").val())
Here is the working fiddle: http://jsfiddle.net/ee3MH/