jdubwelch Posted August 2, 2007 Share Posted August 2, 2007 <script type="text/javascript"> <!-- function addLink (field) { var sampleLink = "Some sample link."; document.form1.field.value = sampleLink; } //--> </script> <form id="form1" name="form1" method="post" action=""> <p><textarea name="textBox1" cols="45" rows="6"></textarea></p> <p><input name="link1" type="button" id="link1" value="add link" onclick="addLink('textBox1')" /></p> <p><textarea name="textBox2" cols="45" rows="6"></textarea></p> <p><input name="link2" type="button" id="link2" value="add link" onclick="addLink('textBox2')" /></p> </form> All i want is: when you click link1 the sample link goes into textbox1, when you click link2 the sample link goes into textBox2. What am I doing wrong here? Quote Link to comment Share on other sites More sharing options...
Philip Posted August 2, 2007 Share Posted August 2, 2007 Try this instead of: <html> <head> <script type="text/javascript"> function addLink (fieldx) { var sampleLink = "Some sample link."; document.forms["form1"].elements[fieldx].value = sampleLink; } </script> </head> <body> <form id="form1" name="form1" method="post" action=""> <p><textarea name="textBox1" cols="45" rows="6"></textarea></p> <p><input name="link1" type="button" id="link1" value="add link" onclick="addLink('textBox1')" /></p> <p><textarea name="textBox2" cols="45" rows="6"></textarea></p> <p><input name="link2" type="button" id="link2" value="add link" onclick="addLink('textBox2')" /></p> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
DeadEvil Posted August 2, 2007 Share Posted August 2, 2007 Get by ID... <script type="text/javascript"> <!-- function addLink(id) { var sampleLink = "Some sample link."; document.getElementById(id).value = sampleLink; } //--> </script> <form id="form1" name="form1" method="post" action=""> <p><textarea name="textBox1" cols="45" rows="6" id="textBox1"></textarea></p> <p><input name="link1" type="button" id="link1" value="add link" onclick="addLink('textBox1')" /></p> <p><textarea name="textBox2" cols="45" rows="6" id="textBox2"></textarea></p> <p><input name="link2" type="button" id="link2" value="add link" onclick="addLink('textBox2')" /></p> </form> Quote Link to comment Share on other sites More sharing options...
Philip Posted August 2, 2007 Share Posted August 2, 2007 Okay, now that is really frustrating, haha. I did that same code last night, and it wasn't working. I tried every way to make it work with get by ID, and it just wouldn't work. So I went old school and did it the long way, haha. Now it works Quote Link to comment Share on other sites More sharing options...
jdubwelch Posted August 2, 2007 Author Share Posted August 2, 2007 Thanks, it works. That was driving me crazy. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.