Results 1 to 4 of 4

Thread: Toggle Text in Textarea

  1. #1
    Join Date
    Jul 2007
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Toggle Text in Textarea

    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.

    Code:
    <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>

  2. #2
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    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?
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

  3. #3
    Join Date
    Jul 2007
    Posts
    34
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I found the answer in the interim. Thanks.

    Code:
    <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>

  4. #4
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    Please write unobtrusive JavaScript. Do you understand how to convert your posted code?
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •