SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Guru pinch's Avatar
    Join Date
    Mar 2005
    Posts
    688
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Insert Text into <textarea> on Client

    I'm working on a page where I have a <textarea> control and several hyperlinks. I want to be able to enter text into the <textarea> simply by typing, but I also want to be able to insert text into the textarea by clicking on the hyperlinks (The tokens which will be inserted into the textarea by clicking on the hyperlinks will be replaced by dynamic data when the data in the textarea is processed by the server).

    Code HTML4Strict:
    <textarea></textarea>
    <a href="#">[Token1]</a>
    <a href="#">[Token2]</a>
    <a href="#">[Token3]</a>

    What I want is to insert the text inside the <a> tags into the textarea at the last place the cursor was located. Is this possible?

  2. #2
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Location
    Bel Air, Maryland.
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HTML Code:
    <script language=Javascript>
    function addTxt(txt, field)
    {
    	var myTxt = txt;
    	var id = field;
    	document.getElementById(id).value += " " + myTxt;
    }
    </script>
    
    <p><a href="#" onclick="addTxt('this is some text!', 'info')">Click here to add Text</a></p>
    <p><a href="#" onclick="addTxt('this is different text!', 'info')">Click here to add different Txt</a></p>
    
    <form name="myForm">
    <textarea cols="30" rows="4" id="info">Blah blah blah my text goes here</textarea>
    </form>
    eh?
    ---
    Paul S. Smith
    technetic | design & code


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
  •