Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

adding params to attachEvent

Posted on 2002-07-17
13
Medium Priority
?
329 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
This article discusses how to create an extensible mechanism for linked drop downs.
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…

670 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