Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2002
    Location
    London, UK
    Posts
    123

    Angry Simple JavaScript Problem

    Hello,

    I've been trying to create a simple JavaScript function that sets the value of a textbox to a specified string. I'm very new to JS and am having some problems!

    Here's the code ..

    <html>

    <head>
    <title>Test JS Page</title>

    <script>

    function SetValue(mainbox_name , input) {
    document.sample.mainbox_name.value = input
    }

    </script>

    </head>

    <body>

    <form method="POST" name="sample">
    <p><input type="text" name="mainbox" size="20"></p>
    <p><a href="javascript:SetValue('mainbox','Chris');">Chris</a></p>
    <p><a href="javascript:SetValue('mainbox','Black');">Black</a></p>
    </form>

    </body>

    </html>

    Basically, click 'Chris' and the textbox called mainbox is populated with 'Chris'. Likewise, click 'Black' and the textbox called mainbox is populated with 'Black'. I need to be able to specify the name of the textbox in question as this function will be used on a page with many textboxes with different names.

    Any suggestions would be really appreciated.

    Cheers

    Chris
    Chris Black | Co-Founder | Traffic Truffle
    traffictruffle.com | We tell you which businesses have visited your website

  2. #2
    Join Date
    Oct 2004
    Location
    Moji
    Posts
    2,107
    Using ID might be a better solution:
    HTML Code:
    <html>
    
    <head>
    <title>Test JS Page</title>
    
    <script language="javascript" type="text/javascript">
    
    function SetValue(mainbox_name , input) {
    document.getElementById(mainbox_name).value = input;
    }
    
    </script>
    
    </head>
    
    <body>
    
    
    <form method="POST" name="sample">
    <p><input type="text" name="mainbox" id="mbox" size="20"></p>
    <p><a href="javascript:SetValue('mbox','Chris');">Chris</a></p>
    <p><a href="javascript:SetValue('mbox','Black');">Black</a></p>
    </form>
    
    </body>
    
    </html>
    DigiPun.ch
    An Awesome Digital Punch Clock
    Release date: October 1st

  3. #3
    Join Date
    Nov 2002
    Location
    London, UK
    Posts
    123
    Great. Thank you!
    Chris Black | Co-Founder | Traffic Truffle
    traffictruffle.com | We tell you which businesses have visited your website

Posting Permissions

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