Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

removing selected words from a textarea 1

Status
Not open for further replies.

TFfan

Programmer
Jan 18, 2002
192
CA
I want my script to take a block of text from a textarea and remove any words from it that appear in an input box with a push of a button. When I run it all it does is put a space at the beginning of the text. I'm stumped.

Thanks!

function refreshtext2(input)
{

var goodArr="";
words = input.value.split(" ");
badw = omit.bwords.value.split(" ");

for (var i=0; i<words.length; i++)
{

if (check(words))
{
goodArr = goodArr + &quot; &quot; + words;
}
}

input.value = goodArr;
arr = goodArr;
}

function check(word)
{

for (var i=0; i < badw; i++)
{
if (word == badw)
{
return false;
}

}

return true;
}
 
It looks to me like you are using the entire array instead of the individual words. Perhaps I don't understand what you are doing but shouldn't it be:
Code:
if (check(words[i]))
     {
      goodArr = goodArr + &quot; &quot; + words[i];
     }
Then you need to change the 'if' in 'check' to use 'badw' Also note that 'goodArr' is not an array but rather a string of words seperated by spaces.
 
That's strange, but it's what I have. Somehow when I was editing it to post here I lost the variable '', but it is in my code that I run. At any rate, the problem is the same. Sorry, for wasting your time in the original post.

function refreshtext2(input)
{

var goodArr=&quot;&quot;;
words = input.value.split(&quot; &quot;);
badw = omit.bwords.value.split(&quot; &quot;);

for (var i=0; i<words.length; i++)
{

if (check(words))
{
goodArr = goodArr + &quot; &quot; + words;
}
}

input.value = goodArr;

}

function check(word)
{

for (var i=0; i < badw; i++)
{
if (word == badw)
{
return false;
}

}

return true;
}
 
After an entire morning of pulling my hair out, I came back from lunch and figured out the obvious problem:

function check(word)
{

for (var i=0; i < badw.length; i++)<---forgot the .length
{
if (word == badw)
{
return false;

Thanks just the same!
 
You can probably make this way, way easier with regexps (probably faster too, no for loops) :)

Put all this in a new .html file and try it out:
Code:
<!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;[URL unfurl="true"]http://www.w3.org/TR/html4/loose.dtd&quot;>[/URL]
<html>
<head>
<title>testbed</title>
<script language=&quot;javascript&quot;>
<!--
function removeVals() {
	//get reference objects for text field and textarea
	var wordObj = document.getElementById(&quot;wordBox&quot;);
	var textObj = document.getElementById(&quot;textBox&quot;);
	
	//create RegExp with string matching exactly the word in wordBox 
	// note: reg exp pattern \b needs to be string-escaped as \\b
	var pattern = &quot;\\b&quot; + wordObj.value + &quot;\\b&quot;;
	var reSearch = new RegExp(pattern, &quot;gim&quot;);
	
	//get value of textarea
	var strTextBox = textObj.value;	

	//replace all matches with nothing
	strTextBox = strTextBox.replace(reSearch, &quot;&quot;);
	
	//put back into textarea
	textObj.value = strTextBox;
}
//-->
</script>
</head>
<body>
<p>
Word to remove: <input type=&quot;text&quot; id=&quot;wordBox&quot; name=&quot;wordBox&quot; size=&quot;20&quot;>
<br>
Textarea to remove from:<br>
<textarea id=&quot;textBox&quot; name=&quot;textBox&quot; rows=&quot;4&quot; cols=&quot;72&quot;>This is your big 'ole paragraph to remove words from.
Change at will.</textarea>
<br>
<input type=&quot;button&quot; name=&quot;remBtn&quot; value=&quot;Do Removing&quot; onclick=&quot;removeVals();&quot;>
</p>
</body>
</html>
Tested in IE6 and NN7.
Don't forget to put a space after your word if you want to get rid of the space too :)

Posting code? Wrap it with code tags: [ignore]
Code:
[/ignore][code]CodeHere
[ignore][/code][/ignore].
 
Thanks Clarkin, never heard of regexps--as you probably could have guess from my rudimentary script. More reading and practise, I suppose. Thanks for the time!
 
As an aside, I think the reason you are losing some code characters is because you need to enclose the code portion in
Code:
tags in your message.
 
Thanks for the head's up and the help!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top