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.

Link to comment
Share on other sites

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

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.