passing a reference to an object in javascript

How do a pass a reference of a textbox into a function so that the function can change the value of the textbox when called?

I know that the 'this.value' object will work in in the tag but I do I change it in the function?
gwaltersiiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mayhewCommented:
If I understand your question right and correctly assume that you're using javascript, then I think the following will help:


<form name="myform">
  <input type="textbox" name="test" value="start">
  <input type="button" name="go" value="Go" onclick="ChangeValue()">
</form>


<script language=javascript>
function ChangeValue(){
  document.forms[0].test.value = "Hi there!";
}
</script>


Let me know if that's what you're looking for.
0
gwaltersiiAuthor Commented:
Actually, what I have are several textboxes (ie: test, test1, test2, test3)that need to use the same function (changevalue()) that changes the value. So when a user changes focus (onBlur event) validation (changevalue function) occurs and makes the appropriate change to the appropriate text box.
0
mayhewCommented:
Ah, there are a couple of ways to do that.


Method 1 (Pass a reference to the object and manipulate it).
=============================
<form name="myform">
  <input type="textbox" name="test1" value="start">
  <input type="button" name="go" value="Go" onclick="AddParens(document.forms[0].test1)"> <br><br>

  <input type="textbox" name="test2" value="start">
  <input type="button" name="go" value="Go" onclick="AddParens(document.forms[0].test2)"> <br><br>

  <input type="textbox" name="test3" value="start">
  <input type="button" name="go" value="Go" onclick="AddParens(document.forms[0].test3)"> <br><br>
</form>


<script language=javascript>
function AddParens(box){
      str = box.value;
      str = '(' + str + ')';
      box.value = str;
}
</script>



Method 2 (Pass a string with the name of the text box and use EVAL to dynamically create code in your function.)
======================================
<form name="myform">
  <input type="textbox" name="test1" value="start">
  <input type="button" name="go" value="Go" onclick="AddParens('test1')"> <br><br>

  <input type="textbox" name="test2" value="start">
  <input type="button" name="go" value="Go" onclick="AddParens('test2')"> <br><br>

  <input type="textbox" name="test3" value="start">
  <input type="button" name="go" value="Go" onclick="AddParens('test3')"> <br><br>
</form>


<script language=javascript>
function AddParens(box){
      str = eval('document.forms[0].' + box + '.value');
      str = '(' + str + ')';
      eval('document.forms[0].' + box + '.value = str');
}
</script>


Let me know if this helps.
0
gwaltersiiAuthor Commented:
Perfect!! I just needed to know how to pass the reference into the function. Thanks!
0
mayhewCommented:
Glad that helped.  :)

Which method do you think you'll tend to use?  
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.