Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Forcing properties to be passed by referece?

Status
Not open for further replies.

MMage

Programmer
Apr 29, 2002
15
US
I'm working on a verification program for a form, I have the code for how to verify each value completed but I'm trying to put the code into a function to make it reusable.

The way I have the code written right now if the user enters a value that is not suitable it changes the visibility of a span tag from 'hidden' to 'visible'.

This works fine if I tell it exactly where to go e.g. tagname.style.visibility = 'visible'.

However when you pass tagname.style.visibility (or any other property It seems) as an argument to a function it only passes the value and not the referance to the variable itself.

Is there any easy way to pass the visibility property like a referance so that I can set the style.visibility property of a tag within the function without explicitly stating which tag but using the arguement passed to the function?

Here is the code I have written you may be able to get a better idea of what I'm trying to do by looking at it:


function verifyInt(intValue, errorVisability) {
if ((intValue != "") && ((isNaN(intValue)) || intValue != parseInt(intValue))) {
errorVisability = 'visible';
}
else {
errorVisability = 'hidden';
}
}
verifyInt(document.form1.dive.value, dive_error.style.visibility);


Thank you in advance for any help offered.
 
The way it is scripted, errorVisibility seems to be intended as out-param. It won't do. You can pass the element object itself (I suppose dive_error be) and assign the specific property of its style.
[tt]
function verifyInt(intValue, obj) {
if ((intValue != "") && ((isNaN(intValue)) || intValue != parseInt(intValue))) {
obj.style.visibility = 'visible';
}
else {
obj.style.visibility = 'hidden';
}
}
verifyInt(document.form1.dive.value, dive_error);
[/tt]
 
I agree with tsuji, it needs the object to be passed to the function parameter but not the object's property.

dive_error[/blue] is an object.

dive_error.style.visibility[/blue] refers to its property.
 
I agree with tsuji, it needs the object to be passed to the function parameter but not the object's property.

dive_error is an object.

dive_error.style.visibility refers to its property.
 
Thank you. Passing the object itself worked and will allow me to reuse this code for the other int feilds in the form.

I wish I could have come up with this on my own and not have to bother you guys. I guess you learn these things as you go... again thank you very much for your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top