Solved

adding params to attachEvent

Posted on 2002-07-17
13
327 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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
 
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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…
Suggested Courses

630 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