PDA

View Full Version : Toggle Text in Textarea



theflyingminstrel
07-12-2009, 02:37 PM
Hi, this JS code adds text to the textarea from links with a set value. Is it possible for the links to toggle the target text (add/remove) yet still append if the other link is clicked?

Thanks.


<html>
<head>

<script>

function text(t){

a = document.getElementById('tarea');

a.value += t;

}
</script>

</head>
<body>


<textarea name="tarea" id="tarea"></textarea>

<br><br>

<a href="javascript:text('this is some text ')">Click Me</a>

<a href="javascript:text('this is some more text ')">Click Me 2</a>


</body>
</html>

Jesdisciple
07-12-2009, 07:46 PM
You lost me... I'm sure that what you're asking is possible, I just don't know exactly what it is you're asking.

When should text be toggled, and when should it be appended? Do the cases overlap?

theflyingminstrel
07-12-2009, 08:07 PM
I found the answer in the interim. Thanks.


<html>
<head>

<script type = "text/javascript">

function showtext() {
var output = "";
if (document.getElementById("chk1").checked) {
output = output + "This is some text" + "\n";
}
if (document.getElementById("chk2").checked) {
output = output + "This is some more text" + "\n";
}
if (document.getElementById("chk3").checked) {
output = output + "This is yet more text" + "\n";
}

document.getElementById("tarea").value = output;
}

</script>

</head>
<body>

<textarea name="tarea" id="tarea" rows = 10 cols = 40></textarea>
<br><br>
Click Me 1 <input type = "checkbox" id = "chk1" onclick = "showtext()"><br>
Click Me 2 <input type = "checkbox" id = "chk2" onclick = "showtext()"><br>
Click Me 3 <input type = "checkbox" id = "chk3" onclick = "showtext()"><br>
</body>
</html>

Jesdisciple
07-12-2009, 08:15 PM
Please write unobtrusive JavaScript (http://javascript.wikia.com/wiki/Unobtrusive_JavaScript). Do you understand how to convert your posted code?