Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jan 2007
    Thanked 0 Times in 0 Posts

    copying text from a string into an input field

    Hi, I'm a total javascript noob, so I have a rather stupid question;

    How can I copy a small text (tag) from a document into an input box? I want to create a simple tagging function for a news script, and above the input fields for the tags, I want to display all the existing tags. So the user can just click on a tag, and it's automatically entered into the input field, comma seperated.

    Thanks in advance!

  2. #2
    Regular Coder
    Join Date
    Oct 2006
    Thanked 0 Times in 0 Posts
    I'm not quite sure what you mean by the last sentence, but I'll take a shot at helping you:

    A textarea's value and an input's value can be changed using the value property. An anchor (or a button) can do things using the onclick property. Putting those two together, you get:

    PHP Code:
    <textarea id="text"></textarea>
    input type="text" id="text2" />
    a href="#nowhere" onclick="document.getElementById('text').value += 'text1 '; document.getElementById('text2').value += 'text2 ';">click me</a

  3. #3
    New Coder
    Join Date
    Nov 2006
    Thanked 0 Times in 0 Posts
    Hi adastra,

    I think this is what you are looking for. Just try the following,

    td { border:solid 1px red; cursor:hand; color:white; background:blue; font-weight:bold; font-family:Tahoma; font-size:10pt; }
    function add(tag) { document.getElementById('txtA').value += tag + ", "; }
    <span style="font-weight:bold; font-family:Tahoma; font-size:10pt; color:blue;">
    Choose your favourite clubs
    <table border="0" cellpadding="2" cellspacing="10">
    <td onclick="add(this.innerHTML);">Liverpool</td>
    <td onclick="add(this.innerHTML);">Celtic</td>
    <td onclick="add(this.innerHTML);">Inter</td>
    <td onclick="add(this.innerHTML);">Bremen</td>
    <td onclick="add(this.innerHTML);">Deportivo</td>
    <td onclick="add(this.innerHTML);">Ajax</td>
    <textarea id="txtA" rows="10" cols="100">
    Share knowledge, it only expands


Posting Permissions

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