Jump to content

one function to change two textboxes, not working


jdubwelch

Recommended Posts

I want one function to be able to change the values into 2 different textareas, by sending the textarea names to the function.  but it's not working. 

 

function addLink (field) {
var url;
url = prompt("Enter URL below: ", "http://");
linkTitle = prompt("Enter the link title");
document.form1.field.value = "[url=" + url + "]" + linkTitle + "[/url]";

}

<form id="form1" name="form1" method="post" action="">
  <p><textarea name="textBox1" cols="45" rows="6"></textarea></p>
  <p><input name="link" type="button" id="link" value="add link" onclick="addLink('textBox1')" /></p>
  <p><textarea name="textBox2" cols="45" rows="6"></textarea></p>
  <p><input name="link" type="button" id="link" value="add link" onclick="addLink('textBox2')" /></p>
</form>

 

it doesn't write the values into the boxes, unless i had two different functions that had: document.textBox1.field.value

 

what am i doing wrong? i've looked in my book, and all on the web.

the problem is to do with the way your referencing the field name, if you run your code you will see an error saying something along the lines of: document.form1.field is null or not an object, which is telling you that the string variable you passed into the function isn't being interpreted as its still called field, if you change it to be referenced like this

 

	document.form1[field].value = "[url=" + url + "]" + linkTitle + "[/url]";

 

it should fix your problem

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.