Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do you strip html tags in textarea input

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 project (I'm new to JS/jQuery).

This is the fiddle: http://jsfiddle.net/mitchbregs/mWaPz/

I found this website: http://www.hscripts.com/scripts/JavaScript/remove-html-tag.php

I implemented the code as so:

function stripHTML(){
var re = /(<([^>]+)>)/gi;
for (i=0; i < arguments.length; i++)
arguments[i].value=arguments[i].value.replace(re, "")
}

and it still didnt work.

The problem: I need it so that when somebody inputs text into the textarea, if they input something like this "<b>Hi</b>" it would strip the "<b>" tag from the input and leave just the text.

If someone could help me out this it would be great.. Thanks!

like image 624
sgerbhctim Avatar asked Aug 18 '12 22:08

sgerbhctim


2 Answers

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/

like image 176
Cat Avatar answered Sep 24 '22 09:09

Cat


var textAreaValue = "text <br /> more text";
var strippedText = $("<div/>").html(textAreaValue).text();​

And here's a Fiddle.

like image 26
João Silva Avatar answered Sep 21 '22 09:09

João Silva