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?
I know that the 'this.value' object will work in in the tag but I do I change it in the function?
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(documen t.forms[0] .test1)"> <br><br>
<input type="textbox" name="test2" value="start">
<input type="button" name="go" value="Go" onclick="AddParens(documen t.forms[0] .test2)"> <br><br>
<input type="textbox" name="test3" value="start">
<input type="button" name="go" value="Go" onclick="AddParens(documen t.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.
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(documen
<input type="textbox" name="test2" value="start">
<input type="button" name="go" value="Go" onclick="AddParens(documen
<input type="textbox" name="test3" value="start">
<input type="button" name="go" value="Go" onclick="AddParens(documen
</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'
<input type="textbox" name="test2" value="start">
<input type="button" name="go" value="Go" onclick="AddParens('test2'
<input type="textbox" name="test3" value="start">
<input type="button" name="go" value="Go" onclick="AddParens('test3'
</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.
ASKER
Perfect!! I just needed to know how to pass the reference into the function. Thanks!
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
<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.val
}
</script>
Let me know if that's what you're looking for.