Solved

adding params to attachEvent

Posted on 2002-07-17
13
322 Views
Last Modified: 2007-12-19
I need to attach an image's onclick event to a function.
but I need to send 2 params to that function, how do I do it ?
thanks,
Gury
0
Comment
Question by:GuryTraub
  • 3
  • 3
  • 2
  • +4
13 Comments
 
LVL 33

Expert Comment

by:knightEknight
ID: 7159698
<SCRIPT language='javascript'>

function yourFunction(param1,param2)
{
  // whatever
}

document.images["yourImageName"].onclick=yourFunction;

</script>
0
 
LVL 1

Author Comment

by:GuryTraub
ID: 7159718
nah, it doesnt work..
You need to use the attachEvent method to attach the event to a certain method, and I dont know how to pass parameters to that func.

your way doesnt work , I tried it earlier, you can try it by yourself.
0
 
LVL 3

Expert Comment

by:tejaspurani
ID: 7159776
I was having the same problem, unable to pass parameter to function I used this method to get parameter

obj.attachEvent("onclick", fnAlert);
function fnAlert(){
     alert( window.event.srcElement.id);
     var search = window.event.srcElement.id;
}

post u r code for more help.

T.P.
0
 
LVL 1

Author Comment

by:GuryTraub
ID: 7159784
I didnt quite understand from your code, how you did finally pass the parameters ??

anyway, my code is something like this.

var img = document.createElement("IMG blahblah");
img.attachEvent ("onclick",foo....??????????) <- HERE IS THE PROBLEM

function foo(prm1,prm2)
{
   alert(prm1 + prm2);
}
0
 
LVL 3

Expert Comment

by:tejaspurani
ID: 7159842
I was unable to pass the parameter,
so I used the object id as paremeter.

<img src="xyz.gif" id="prm1-prm2">
img.attachEvent ("onclick",foo....??????????) <- HERE IS
//THE PROBLEM
function foo()
{
var search = window.event.srcElement.id;
var param = search.split("-")
//alert(prm1 + prm2); instead of this use
alert(param(0) + param(1) );
}
T.P.
0
 
LVL 33

Expert Comment

by:knightEknight
ID: 7159854
you could also attach properties to the function ...

function foo()
{
   alert(this.prm1 + this.prm2);
}

foo.prm1 = "Param1";
foo.prm2 = "Param2";
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 33

Expert Comment

by:knightEknight
ID: 7159887
oops, typo ... change this:

   alert(this.prm1 + this.prm2);

to this:

   alert(foo.prm1 + foo.prm2);
0
 
LVL 3

Expert Comment

by:tejaspurani
ID: 7162013
any comment.
T.P.
0
 

Accepted Solution

by:
kwbrow earned 50 total points
ID: 8121379
I had the same problem a while back, this is how I solved it.

img.attachEvent ("onclick",function() {foo(prm1, prm2)});

function foo(prm1,prm2)
{
  alert(prm1 + prm2);
}
0
 
LVL 12

Expert Comment

by:ahosang
ID: 8329805
This question has been abandoned. I will make a recommendation to the moderators on its resolution in a week or so. I appreciate any comments that would help me to make a recommendation.
 
In the absence of responses, I may recommend DELETE unless it is clear to me that it has value as a PAQ. Silence = you don't care
 
ahosang
0
 
LVL 12

Expert Comment

by:ahosang
ID: 8527837
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

points to kwbrow
Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
ahosang
EE Cleanup Volunteer
0
 

Expert Comment

by:Sandmanz
ID: 8548390
If you are attaching an event to an existing object and would like to add params to it, simply use this:

<script>

function myParentFunc () {

   controller = this;
   myParamNestFunc = function() {
        controller.iframe.detachEvent("onload",myParamNestFunc);
        callBackFunctionOutSideNest(param1,param2);
  }
  controller.iframe.attachEvent("onload",myParamNestFunc);
}
</script>


This is a rough script (dont try and execute it as is etc). Essentially this will basically attach the onLoad event to the iframe, and when content is basically refreshed and loaded, the callback routine (myParamNestFunc) will first detach itself from the iframe and make an alternative callback to nomitated method(s) of your choice.
0
 

Expert Comment

by:SpideyMod
ID: 8627045
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
In this article, we'll look how to sort an Array in JavaScript, including the more advanced techniques of sorting a collection of records either ascending or descending on two or more fields. Basic Sorting of Arrays First, let's look at the …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now