Jump to content

[SOLVED] why doesn't this work?


jdubwelch

Recommended Posts

<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? 

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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 :P

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.