Here’s the script:
<script language="javascript" type="text/javascript">
function addtext() {
var newtext = document.myform.inputtext.value;
if (document.myform.placement[1].checked) {
document.myform.outputtext.value = "";
}
document.myform.outputtext.value += newtext;
}
</script>
<form name="myform">
<table border="0" cellspacing="0" cellpadding="5"><tr>
<td><textarea name="inputtext"></textarea></td>
<input type="radio" name="placement" value="append" checked> Add to Existing Text<br>
<td><p><input type="radio" name="placement" value="replace"> Replace Existing Text<br>
<input type="button" value="Add New Text" onClick="addtext();"></p>
</td>
<td><textarea name="outputtext"></textarea></td>
</tr></table>
</form>
This script is going to work great in conjunction with my PHP script however the problem is with the “Add to Existing Text”.
If I have three lines such as:
Apples
Bananas
Oranges
And I "“Add to Existing Text” which has “Strawberries, blueberries” it does so in this fashion:
Strawberries
blueberries Apples
Bananas
Oranges
As oppsed to:
Strawberries
blueberries
Apples
Bananas
Oranges
Does anyone know how to add that <br /> at the beginning if text is being added?
r51
July 3, 2010, 9:49pm
2
<script language="javascript" type="text/javascript">
function addtext() {
var newtext = document.myform.inputtext.value;
if (document.myform.placement[1].checked) {
document.myform.outputtext.value = "";
}
var outText = document.myform.outputtext;
outText.value += outText.value.length > 0 ? "\
" + newtext : newText;
}
</script>
Thanks Felgall…your help is greatly appreciated
Thanks guys, much appreciated. Since were on the topic would it be difficult to make the first instance without a "
". The reason is that if the new textarea is blank it adds a blank space in the first entry. As I’m using the information in this form I’ll have to restructure everything if the first entry is blank.
soma56:
Thanks guys, much appreciated. Since were on the topic would it be difficult to make the first instance without a "
". The reason is that if the new textarea is blank it adds a blank space in the first entry. As I’m using the information in this form I’ll have to restructure everything if the first entry is blank.
document.myform.outputtext.value +=
((document.myform.outputtext.value)?'\
':'')+newtext;
Yes, I think this is a better idea, mostly because if you did manage to add a br, you’d get
<textarea>Strawberries<br/>blueberries<br/>Apples<br/>Bananas<br/>Oranges</textarea>
I don’t believe you can have tags inside a textarea element, and if you can, they should be interpreted as plain text (cdata), not real br’s.
r51
July 3, 2010, 9:02pm
7
How about using a new line escape sequence, "
"?
document.myform.outputtext.value += "\
" + newtext;