...

View Full Version : Append/Replace Text in Textarea Onclick Question



theflyingminstr
07-20-2009, 03:12 PM
Hi the script at the bottom appends a text area with info onclick (ref:http://www.codingforums.com/showthread.php?p=838162).

I wanted to know if it's possible to have a function (possible within the existing one?) that replaces text as well.

Something like this format would be perfect for what I'm tring to do:



<a href="#" onclick = "check('Replace this text', 'With this in the textarea')">Click Me to replace</a><br/>


Original Code:



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
<title>Onclick Textarea</title>

<script type="text/javascript">
function check(txt) {
var obj=document.form1.textarea1;
if(obj.value.search(txt)==-1) {
obj.value+=txt + "\n";
}
else {
if (txt !='This is some more text which cannot be deleted ') {
obj.value=obj.value.replace(txt,"");
obj.value=obj.value.replace(/\.*(\r|\n)+/g,"\n")
}
}

}
</script>

</head>

<body>
<form name="form1">
<textarea rows="10" cols="40" name="textarea1"></textarea>
<br/><br/>

<a href="#" onclick = "check('This is some long-winded text ')">Click Me 1</a><br/>
<a href="#" onclick = "check('This is some more text which cannot be deleted ')">Click Me 2</a><br/>
<a href="#" onclick = "check('This is yet more pointless text ')">Click Me 3</a>

<!--
<a href="javascript:check('This is some long-winded text ')">Click Me 1</a><br/>
<a href="javascript:check('This is some more text which cannot be deleted ')">Click Me 2</a><br/>
<a href="javascript:check('This is yet more pointless text ')">Click Me 3</a>
-->

</form>

</body>
</html>


Thanks!

A1ien51
07-20-2009, 03:27 PM
Basic idea:



function check(oldText,newText){
newText = newText || "";
var ta = document.form1.textarea1;
if(ta.value.indexOf(oldText) === -1){
ta.value += oldText + "\n";
}
else{
ta.value = newText.replace(oldText,newText);
}
}


Eric

Philip M
07-20-2009, 03:41 PM
I am not entirely clear, but is this what you want?


<html>
<head>

<script type="text/javascript">
function check(txt, newTxt) {
var obj=document.form1.textarea1;
if(obj.value.search(txt)==-1) {
obj.value+=txt + "\n";
}
else {
if (txt !='This is some more text which cannot be deleted ') {
obj.value=obj.value.replace(txt,newTxt);
obj.value=obj.value.replace(/\.*(\r|\n)+/g,"\n")
}
}

}
</script>

</head>

<body>
<form name="form1">
<textarea rows="10" cols="40" name="textarea1"></textarea>
<br/><br/>

<a href="#" onclick = "check('This is some long-winded text ', 'Some Other Nonsensical Text')">Click Me 1</a><br/>
<a href="#" onclick = "check('This is some more text which cannot be deleted ', '')">Click Me 2</a><br/>
<a href="#" onclick = "check('This is yet more pointless text ', 'Yet More Incomprehensible Text')">Click Me 3</a>

</body>
</html>

theflyingminstr
07-20-2009, 03:46 PM
I am not entirely clear, but is this what you want?


<html>
<head>

<script type="text/javascript">
function check(txt, newTxt) {
var obj=document.form1.textarea1;
if(obj.value.search(txt)==-1) {
obj.value+=txt + "\n";
}
else {
if (txt !='This is some more text which cannot be deleted ') {
obj.value=obj.value.replace(txt,newTxt);
obj.value=obj.value.replace(/\.*(\r|\n)+/g,"\n")
}
}

}
</script>

</head>

<body>
<form name="form1">
<textarea rows="10" cols="40" name="textarea1"></textarea>
<br/><br/>

<a href="#" onclick = "check('This is some long-winded text ', 'Some Other Nonsensical Text')">Click Me 1</a><br/>
<a href="#" onclick = "check('This is some more text which cannot be deleted ', '')">Click Me 2</a><br/>
<a href="#" onclick = "check('This is yet more pointless text ', 'Yet More Incomprehensible Text')">Click Me 3</a>

</body>
</html>

Yes that's exactly what I was trying to do. Thanks so much again Phillip!

Thanks as well, Eric. Awesome.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum