By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,131 Members | 1,437 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,131 IT Pros & Developers. It's quick & easy.

writing to a coordinate in a textarea

P: 132
Hello everyone,

I've been browsing the net for some time now and I can't find the answer I'm looking for.

I'm trying to insert text in a textarea but at a specified place. 30 characters from the left side of the textarea. But I can't find that script.

What I did found were scripts to insert text at the cursor. The caret position was saved in a variable and then new text inserted at that caret-position. However, I would like to specify the location in the script. Anybody knows how to achieve this?


Jan 8 '08 #1
Share this Question
Share on Google+
6 Replies

P: 428
Expand|Select|Wrap|Line Numbers
  1. var str=' String to insert';
  2. var n=30;
  3. var val= textareareference.value;
  4. textareareference.value= val.substring(0,n)+ str+ val.substring(n);
Jan 8 '08 #2

P: 132
Hi MrHoo,

I've tried the code but it doesn't seem to work. On the net I found not really that much information about it. Could you explain me how the textareareference works ?


Jan 8 '08 #3

P: 428
Excuse me- when I said textareareference, I meant
'put a reference to the textarea here'-
for example, if you had one textarea on the page you could use
or if it had an id of 'mytext' it could be
Jan 8 '08 #4

P: 132
Hi Mrhoo,

Silly me, I didn't think of it that way :-)

I tested it, and it writes the code into the textarea except it writes it in theleft corner and not at the 30th character. Am I doing something wrong?

Expand|Select|Wrap|Line Numbers
  1. <HTML>
  2. <HEAD>
  3. <SCRIPT>
  5. function execute(){
  6. var str='This is a test';
  7. var n=30;
  8. var val = document.form.output.value;
  9. document.form.output.value += val.substring(0,n)+ str + val.substring(n);
  10. }
  11. </SCRIPT>
  13. </HEAD>
  14. <BODY>
  15. <form name="form">
  16. <textarea name="output" cols="50" rows="15"></textarea>
  17. <br>
  18. <input type="button" value="Test it" onclick="execute()">
  19. <input type="reset" value="reset">
  20. </form>
  21. </BODY>
  22. </HTML>
Jan 8 '08 #5

P: 428
If you do not have 30 characters you need to insert them-
and don't use += to set the value here.


Expand|Select|Wrap|Line Numbers
  1. function execute(){
  2.     var str= 'This is a test';
  3.     var n= 30;
  4.     var who= document.getElementsByName('output')[0];
  5.     var val= who.value;
  6.     while(val.length<n)val= ' '+ val;
  7.     who.value= val.substring(0,n)+' '+ str + ' '+
  8.     val.substring(n);
  9. }

<textarea name="output" cols="50" rows="15"></textarea>
<input type="button" value="Test it" onclick="execute()">
<input type="reset" value="reset">
Jan 8 '08 #6

P: 132
Thanks mrhoo,

That works indeed. Except I just came to realise that this probably isn't the right approach for my purpose because I need to put it on every line.

You see I'm generating an output in the textarea and a part of that output I want to align to character 30 just so the contents are clear and structured.

I'll have to experiment further to make it work I think.

But thanks for the help you've given me.

Jan 8 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.