[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 220
  • Last Modified:

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?
0
gwaltersii
Asked:
gwaltersii
  • 3
  • 2
1 Solution
 
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now