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!

Passing Objects To Functioncs

Status
Not open for further replies.

SimonMarkDawson

Programmer
Oct 17, 2001
23
GB
Hi

I'm having trouble passing an object to a Javascript function. I can't use 'this' because I want to pass a different object to the one calling the function. So far I've tried using the object's name with and without ' marks as parameters to the function being called but without success.

Any ideas on the correct syntax would be much appreciated.

Thanks in advance etc

- Simon
 
Sorry - To clarify the above, I want to use a variable in the function call to refer to the other object (ie not just the other object's name) so that I can vary which object is passed at run time.

Thanks again,
- Simon
 
show the code you're using so far...

=========================================================
if (!succeed) try();
-jeff
 
Hi

This is the basis of a PERL script to emphasize my trouble with the Javascript:

print "function chtest(obj1){";
print "document.obj1.innerText=\"Some Different Text\";";
print "}";

$whichobj="test".$no;
print &quot;<div id=&quot;test1&quot; onclick=chobj($whichobj)></div>&quot;;
print &quot;<div id=&quot;test2&quot;>Some Text</div>&quot;;
print &quot;<div id=&quot;test2&quot;>Some Text</div>&quot;;

I want to be able to vary $whichobj within PERL to determine which div gets changed.

Hope that explains my syntax prob. I feel sure that the prob is that $whichobj isn't an object handle but just a variable but how can I use the object with that name in the Javascript?

Thanks

Simon
 
if you're passing in an object, you don't need to specify document.object...

try this:

print &quot;function chtest(obj1){&quot;;
print &quot;obj1.innerText=\&quot;Some Different Text\&quot;;&quot;;
print &quot;}&quot;;

and you should be able to refer to each <div> using &quot;this&quot;:

print &quot;<div id=&quot;test1&quot; onclick=&quot;chobj(this);&quot;></div>&quot;;

=========================================================
if (!succeed) try();
-jeff
 
True enough but the problem is in passing another object other than that from which the function is called - 'this' only passes the object that is calling the function ie in your example you'd change the innerText of &quot;test1&quot; whereas I need to change the innerText of &quot;test2&quot; by clicking on the division &quot;test1&quot; (If that makes any sense!) Also, I don't just want to pass test2 within the function call but to use a variable instead so that this can be changed within PERL.
 
Thanks for the help. Problem now solved by passing string variable then using getElementByName within function to convert to object.

Simon
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top