Link to home
Start Free TrialLog in
Avatar of gwaltersii
gwaltersii

asked on

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?
Avatar of mayhew
mayhew

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.
Avatar of gwaltersii

ASKER

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.
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.
Perfect!! I just needed to know how to pass the reference into the function. Thanks!
ASKER CERTIFIED SOLUTION
Avatar of mayhew
mayhew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial