tags:

views:

37

answers:

2

Hi,

I want to call a function onclick and pass the event object to the function:

 progressBarOuter.onclick = function(e)  { 
     var x; if (!e) {
         x=window.event.clientX;
     } 
      else {
          x = e.clientX
     } 

     yt.clickedOffset = x; yt.progressBarClicked
 }

So i'm assigning clickedOffset to by enclosing object (yt) and then calling progressBarClicked which then uses the clickedOffset var. But what I actually would rather be doing is something like this:

progressBarOuter.onclick = yt.progressBarClicked(e);

Because it's much more compact. Problem is that even if the user has clicked or not this bit of code is executed... yt.progressBarClicked(e).

Is there any way around this?

+1  A: 

Try

progressBarOuter.onclick = yt.progressBarClicked;

But technically this is not the same as your original code. The calculation for the mouse position (clientX) is missing altogether.

Anurag
Yeah this is my problem. I want to do exactly what you've written above but I need to pass the event in...just thought there might be a way to do this that I didn't know about
elduderino
The event will automatically be passed in to your `progressBarClicked` function since it is a callback for a mouse event.
Anurag
This will break the `this` inside `yt.progressBarClicked`, because the function is called from the `window` object.
Ivo Wetzel
@Ivo - yes it will break `this`, but that would only matter if `this` is being used inside `yt.progressBarClicked` in the first place, but good point nonetheless.
Anurag
So how do i access the event properties inside progressBarClicked?
elduderino
Would it just be function progressBarClicked(e) {console.log(e.clientX);} ?
elduderino
+3  A: 

Your:

progressBarOuter.onclick = yt.progressBarClicked(e);

doesn't yield the expected result, because you're not assigning the function yt.progressBarClicked to the onclick handler. You are actually calling the function and therefore assigning its return value to onclick.

The shortest working thing you can get without breaking anything is this:

progressBarOuter.onclick = function(e){
    yt.progressBarClicked((e || window.event).clientX); // pass the x position as a parameter
};

If you don't wrap it, yt.progressBarClicked will be called from the window object and therefore the value of this inside the function will be also set to the window object.

Of course you could also handle the calculation of the x position inside the function and just pass the event to it:

progressBarOuter.onclick = function(e){yt.progressBarClicked(e);};
Ivo Wetzel
Hi, Yes I completely understand what's going on just wondered if there was a different method I could use to avoid the extra anonymous function...however this progressBarOuter.onclick = function(e){yt.progressBarClicked(e);};seems like an elegant enough solution. Thanks!
elduderino
actually just doing progressBarOuter.onclick = yt.progressBarClicked; was enough...the progressBarClicked receives the event
elduderino