Results 1 to 3 of 3
Thread: Simple JavaScript Problem
-
11-03-2005, 07:27 AM #1WHT Addict
- Join Date
- Nov 2002
- Location
- London, UK
- Posts
- 123
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
ChrisChris Black | Co-Founder | Traffic Truffle
traffictruffle.com | We tell you which businesses have visited your website
-
11-03-2005, 07:50 AM #2DigitalPunchCardMaker
- 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>
-
11-03-2005, 07:52 AM #3WHT Addict
- 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