Forum Moderators: open

Message Too Old, No Replies

Adding Line Breaks in <textarea>

         

Patch

7:09 am on Aug 25, 2004 (gmt 0)

10+ Year Member



I have a script that inserts a string into a textarea but I also want it to insert a line break in the textarea, does anyone know how to do this?

"\n" '\n' \n - don't work. Neither does \r or \r\n. They just stop the entire funtion working (Mozillia says the function is not defined)

Can anyone help?

Bernard Marx

7:29 am on Aug 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It should work. Can we see the function, or the section that puts in the line breaks?

Patch

7:34 am on Aug 25, 2004 (gmt 0)

10+ Year Member



Thanks,


function reply(number) {
var cache = document.comment.comment.value;
var addition = "[reply" + number + "]\n";
document.comment.comment.value = cache + addition;
document.comment.comment.focus();
}

Bernard Marx

12:30 pm on Aug 25, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<html><head><title>line breaks</title>

<script type="text/javascript">
// handy proto method
String.prototype.trim = function(){ return this.replace(/(^\s+)¦\s+$/g,"")}

function reply(number)
{
number = number.trim() // remove lead + trail spaces
if(!number) return // if empty string, stop
var area = document.comment.commenttext
var addition = "[reply" + number + "]\n";
area.value += addition;
area.focus();
}
</script>

</head>
<body>

<form name = "comment">
<textarea name="commenttext"></textarea>
<!-- test stuff -->
<br>
<input type="text" name="testno">
<input type="button" value="test" onclick="reply(this.form.testno.value)">
<input type="button" onclick="this.form.reset()" value="reset">
<!-- / -->
</form>
<br>
<div style="font-family:verdana;font-size:13px; width:400px;">
<p>
#1 Best not call the form and an element by the same name
(..it looks like you are).
For reasons..., you can get away with it, but it's generally
not so good an idea. :)</p>
<p>
#2 As the new input is added below the last,
you don't need the cache. You can simply
append the input onto the current contents.</p>
</div>
</BODY></html>