writing to a coordinate in a textarea

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Cainnech
    New Member
    • Nov 2007
    • 132

    writing to a coordinate in a textarea

    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?

    Thanks,

    Cainnech
  • mrhoo
    Contributor
    • Jun 2006
    • 428

    #2
    [CODE=javascript]
    var str=' String to insert';
    var n=30;
    var val= textareareferen ce.value;
    textareareferen ce.value= val.substring(0 ,n)+ str+ val.substring(n );
    [/CODE]

    Comment

    • Cainnech
      New Member
      • Nov 2007
      • 132

      #3
      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 textareareferen ce works ?

      Thanks,

      Cainnech

      Comment

      • mrhoo
        Contributor
        • Jun 2006
        • 428

        #4
        Excuse me- when I said textareareferen ce, I meant
        'put a reference to the textarea here'-
        for example, if you had one textarea on the page you could use
        document.getEle mentsByTagName( 'textarea')[0],
        or if it had an id of 'mytext' it could be
        document.getEle mentById('mytex t').

        Comment

        • Cainnech
          New Member
          • Nov 2007
          • 132

          #5
          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?

          Code:
          <HTML>
          <HEAD>
          <SCRIPT>
          
          function execute(){
          var str='This is a test';
          var n=30;
          var val = document.form.output.value;
          document.form.output.value += val.substring(0,n)+ str + val.substring(n);
          }
          </SCRIPT>
          
          </HEAD>
          <BODY>
          <form name="form">
          <textarea name="output" cols="50" rows="15"></textarea>
          <br>
          <input type="button" value="Test it" onclick="execute()">
          <input type="reset" value="reset">
          </form>
          </BODY>
          </HTML>

          Comment

          • mrhoo
            Contributor
            • Jun 2006
            • 428

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

            <HTML>
            <HEAD>
            <SCRIPT>

            [CODE=javascript]function execute(){
            var str= 'This is a test';
            var n= 30;
            var who= document.getEle mentsByName('ou tput')[0];
            var val= who.value;
            while(val.lengt h<n)val= ' '+ val;
            who.value= val.substring(0 ,n)+' '+ str + ' '+
            val.substring(n );
            }[/CODE]
            </SCRIPT>

            </HEAD>
            <BODY>
            <form>
            <textarea name="output" cols="50" rows="15"></textarea>
            <br>
            <input type="button" value="Test it" onclick="execut e()">
            <input type="reset" value="reset">
            </form>
            </BODY>
            </HTML>
            Last edited by mrhoo; Jan 8 '08, 02:27 PM. Reason: name change

            Comment

            • Cainnech
              New Member
              • Nov 2007
              • 132

              #7
              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.

              Cainnech

              Comment

              Working...