Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

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

adding params to attachEvent

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
GuryTraub
Asked:
GuryTraub
  • 3
  • 3
  • 2
  • +4
1 Solution
 
knightEknightCommented:
<SCRIPT language='javascript'>

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

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

</script>
0
 
GuryTraubAuthor Commented:
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
 
tejaspuraniCommented:
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
GuryTraubAuthor Commented:
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
 
tejaspuraniCommented:
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
 
knightEknightCommented:
you could also attach properties to the function ...

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

foo.prm1 = "Param1";
foo.prm2 = "Param2";
0
 
knightEknightCommented:
oops, typo ... change this:

   alert(this.prm1 + this.prm2);

to this:

   alert(foo.prm1 + foo.prm2);
0
 
tejaspuraniCommented:
any comment.
T.P.
0
 
kwbrowCommented:
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
 
ahosangCommented:
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
 
ahosangCommented:
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
 
SandmanzCommented:
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
 
SpideyModCommented:
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange
0

Featured Post

Receive 1:1 tech help

Solve your biggest tech problems alongside global tech experts with 1:1 help.

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