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
https://forums.phpfreaks.com/topic/62990-solved-why-doesnt-this-work/
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>

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>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.