Strip all tags when pasted to textbox
I've got a textbox(Rich textbox Control - see FreeTextBox.com) on my page.
Say I cut some text that is HTML encoded and paste it into my textbox. The HTML is pasted along with the text in my textbox. I do not want this HTML to tag along when pasted.
I'm hoping that there is some existing javascript out there that will strip all of these tags upon pasting into the textbox.
Anyone have the goods?
-MP
P.S. - Happy New Year (of the Ox) to those of you celebrating
Re: Strip all tags when pasted to textbox
Not sure if tag referencing (not sure what it's called) is what you are after:
http://www.vbforums.com/showthread.php?t=549848
Re: Strip all tags when pasted to textbox
Say I have an empty textbox.
I paste the following in the textbox which was copied from an HTML document:
Larry
In fact, "Larry" is HTML formatted. When the code behind "Larry" is viewed, the following is revealed:
<div class="name">Larry</div>
I want to stripall tags associated with "Larry" whenever I paste to my textbox. There has got to be a way to do this with Javascript.
Re: Strip all tags when pasted to textbox
Quote:
Originally Posted by Ms.Longstocking
In fact, "Larry" is HTML formatted. When the code behind "Larry" is viewed, the following is revealed:
<div class="name">Larry</div>
I want to stripall tags associated with "Larry" whenever I paste to my textbox. There has got to be a way to do this with Javascript.
Ah ok! I was thinking you was doing via a textbox which, unfortunately has the <input> </input> tags because it is not a stand alone element but part of a form.
I have written the code to return the contains of the div:
Code:
<html>
<!--Code to get the text on its own.-->
<div id="text">Larry</div>
<script>
var text
text=document.getElementById("text");
text.html='<div>';
alert(text.innerHTML);
</script>
</html>
Do you want me to add a copy function to it?
Re: Strip all tags when pasted to textbox
Actually, it's more complicated than that.
Let's say there is already formatted text in the textbox (but stuff I'd want to keep). What then?
The only thing I can think of is to get the current cursor position within the textbox (position A), then get the cursor position after the text is pasted(position B), and only perform the function on the newly pasted stuff (from position A to position B)
Am I on the right track here? Or is there an easier way to perform the function on only the text that is being pasted?
Re: Strip all tags when pasted to textbox
Going back to post #3 I'm understanding that you want to remove:
Code:
<div class="name"> </div>
and just have "Larry" in the textbox! However, that brings me to another question since the tags are required for formatting since you removed the tags in the first step all you will be left with is formatted text.
Re: Strip all tags when pasted to textbox
Yes I'd like to remove the DIV tag without removing the "Larry".
Everything that is pasted to the textbox should be unformatted by the javascript. However, the text that is already in the textbox should maintain its existing formatting. Savvy? :)
Re: Strip all tags when pasted to textbox
Ok!
I just wrote this code to change the colour of the text in the div:
Code:
<style>
div {
color:#0033CC}
input {
margin-left:: 10px;
color:#00FF66}
</style>
Now together with the code I posted in post #4 it will give the text in the div a blue colour and the textbox text a green colour. However, you paste the text into the text it will take on the style of the text that is already in the text box. Is that the result you want?
Edit:
Forgot the text box:
Code:
<form>
<input name="text1" type="text" value="hello"/>
</form>
Re: Strip all tags when pasted to textbox
Hi there Ms.Longstocking,
does this help...
Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
if(window.addEventListener){
window.addEventListener('load',stripHTMLtags,false);
}
else {
if(window.attachEvent){
window.attachEvent('onload',stripHTMLtags);
}
}
function stripHTMLtags(){
df=document.forms[0];
tags=/<[^>]+>/g;
df[1].onclick=function() {
matches=df[0].value.match(tags);
if((df[0].value=='')||(matches==null)){
alert('there are no tags to remove!!!');
return;
}
df[0].value=df[0].value.replace(tags,'');
}
}
</script>
</head>
<body>
<form action="#">
<div>
<textarea rows="20" cols="60"></textarea>
<input type="button" value="strip tags">
</div>
</form>
</body>
</html>