1

Here's what I'm trying to do :

I have a page with some links. Most links have a function attached to them on the onclick event.

Now, I want to set a css class to some links and then whenever one of the links is clicked I want to execute a certain function - after it returns , I want the link to execute the onclick functions that were attached to it.

Is there a way to do what I want ? I'm using jQuery if it makes a difference.

Here's an attempt at an example :

$("#link").click(function1);
$("#link").click(function2);
$("#link").click(function(){
   firstFunctionToBeCalled(function (){
      // ok, now execute function1 and function2
   });
}); // somehow this needs to be the first one that is called


function firstFunctionToBeCalled(callback){
    // here some user input is expected so function1 and function2 must not get called
    callback();

}

All this is because I'm asked to put some confirmation boxes (using boxy) for a lot of buttons and I really don't want to be going through every button.

sirrocco
  • 7,975
  • 4
  • 59
  • 81

4 Answers4

1

If I understand you correctly, is this wat you wanted to do..

var originalEvent = page.onclick;   //your actual onclick method
page.onclick = handleinLocal;       //overrides this with your locaMethod

function handleinLocal()
{     ...your code...     
   originalEvent (); 
 // invoke original handler
}
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
RameshVel
  • 64,778
  • 30
  • 169
  • 213
0

I would use jQuery's unbind to remove any existing events, then bind a function that will orchestrate the events I want in the order I want them.

Both bind and unbind are in the jQuery docs on jquery.com and work like this...

$(".myClass").unbind("click"); // removes all clicks - you can also pass a specific function to unbind

$(".myClass").click(function() {
    myFunctionA();
    myFunctionB($(this).html()); // example of obtaining something related to the referrer
});
Fenton
  • 241,084
  • 71
  • 387
  • 401
  • What about the onclick function that was attached to the element? – rahul Sep 14 '09 at 12:35
  • Exactly, the thing is that every link has a different method/methods - and the mechanism needs to be generic - so I can apply it to any element. – sirrocco Sep 14 '09 at 12:46
0

An ugly hack will be to use the mousedown or mouseup events. These will be called before the click event.

kgiannakakis
  • 103,016
  • 27
  • 158
  • 194
  • Yes, but when that gets clicked, I need to execute a function and on that functions callback - continue to run all it's click attached functions. – sirrocco Sep 14 '09 at 12:54
0

If you can add your event handler before the rest of handlers, you could try to use jQuery's stopImmediatePropagation

phuzi
  • 12,078
  • 3
  • 26
  • 50
Serxipc
  • 6,639
  • 9
  • 40
  • 51