PDA

View Full Version : How to append text in a textarea without replacing the original text??



shachi
08-11-2006, 05:51 PM
Can anyone tell me how do I append text in a textarea element without replacing the original data??
What I mean is if I have this text in a textarea:



Test1
Test2


Now how do I append text which may end up like this:



Test1
Test2
Test3


The traditional formname.formelement.value isn't working for me it just replaces the whole thing inside the element. Any help would be greatly appreciated.:)

mburt
08-11-2006, 05:58 PM
<html>
<head>
<script>
function addtxt(input) {
var obj=document.getElementById(input)
var txt=document.createTextNode("blah blah")
obj.appendChild(txt)
}
</script>
</head>
<body>
<textarea id="textarea1"></textarea>
<br><input type="button" value="Write blah blah" onclick="addtxt('textarea1')">
</body>
</html>

shachi
08-11-2006, 06:08 PM
Thanks, worked great.

mburt
08-11-2006, 06:10 PM
You could also do this:



<html>
<head>
<script>
function addtxt(input) {
var obj=document.getElementById(input)
obj.value+="blah test 123"
}
</script>
</head>
<body>
<textarea id="textarea1"></textarea>
<br><input type="button" value="Write blah blah" onclick="addtxt('textarea1')">
</body>
</html>

shachi
08-11-2006, 06:24 PM
Ah yes!! Now I remember I know the second technique but never used it. ;) Thanks anyways.

Twey
08-11-2006, 07:04 PM
.value is the better technique here unless you've got one heck of a lot of text (1MB or more, probably).

shachi
08-11-2006, 07:19 PM
I know I need to post this as a new thread but I am too lazy to do that. So, can anyone tell me how do I make an event fire when the user presses enter from a form ?? I have a basic form which does some ajax request something like this:



<form name="chatform">
<textarea name="chatresp" cols="50" rows="20" id="showresults" readonly="readonly"></textarea><br>
<input type="text" name="text" id="text"><input type="button" value="Send" onclick="send();">
</form>


Now I need it to execute send(); even when the user simply hits enter and doesn't click the "Send" button. Thanks.

EDIT: Forgot to mention can anyone also tell me how do I scroll to the bottom of a textarea which gets dynamically updated??

Twey
08-11-2006, 07:50 PM
You have to make the button a submit button and use onsubmit on the form.

shachi
08-11-2006, 07:52 PM
I can't use submit button in a ajax form can I?? If I do then the form will be submitted(meaning the page will reload).

shachi
08-11-2006, 08:14 PM
Never mind, found out the solution. I used return false.