SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Sep 2004
    Location
    Kansas City, MO
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript OnClick Hyperlink to Populate Form Field?

    First off, I want to apoloigze if this has already been answered ... the SP search feature is taking about 2 mins per search, so it was making it difficult to search the archive ...

    Anyways, on to my question ...

    I have a form such as:

    Code:
    <form name="additem" method="post" enctype="multipart/form-data" action="index.php">
    <input name="itemname" type="text" id="itemname" size="40">
    </form>
    And I'm wanting to be able to click on a hyperlink to populate the "itemname" input field ... such as

    Code:
    <a href="#" onclick="CopyDetails('HERE IS THE TEXT I WANT COPIED',additem,itemname)">Copy to form</a>
    I know CopyDetails is not a JS function, but it was just part of the example I came up with.

    The hyperlinks that I will be clicking will be on the same page as the form.

    Any thoughts or ideas?


    - Scott

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Code Javascript:
    function init() {
      document.getElementById('copydetails').onclick = copyDetails('Copy this text', 'itemname');
      // other things to do onload
    }
     
    function copyDetails(text, field) {
      document.getElementById(field).value = text;
    }
     
    window.onload = init;
    Not necessary to have all those functions if this is the only javascript you need, but I'm guessing you might need to populate other form fields, so this should get you started. Also, there's no need for messy inline Javascript in the HTML, so your link would just be:
    HTML Code:
    <a href="#" id="copydetails">Copy to form</a>

  3. #3
    SitePoint Member
    Join Date
    Sep 2004
    Location
    Kansas City, MO
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Raffles ...

    You Rock!

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2006
    Location
    Bel Air, Maryland.
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code JavaScript:
    <script language="javascript/text">
    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>
    <form name="test">
    <input type="text" id="info" value="">
    </form>

    Well dang, a little too late.
    ---
    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
  •