Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

can you bind .click() so that it fires prior to onclick?

I'm writing jQuery for an app that's being build in JSF. The JSF components are using a lot of their own JS for doing whatever JSF does and they use a lot of onclick attributes to handle it all.

Is there valid/proper way to bind your own click event to an element and ensure that your event fires prior to the default onclick event? It appears that by default a jQuery click function is fired after any inline onclick function calls.

like image 585
DA. Avatar asked Jul 14 '11 14:07

DA.


People also ask

Does Onclick fire before href?

onclick does trigger first, the href only goes once onclick has returned! Are you doing something asynchronous in the onclick and you want to wait until it returns or something?

How do you fire a click event on an element?

The HTMLElement. click() method simulates a mouse click on an element. When click() is used with supported elements (such as an <input> ), it fires the element's click event. This event then bubbles up to elements higher in the document tree (or event chain) and fires their click events.

How can I trigger a click event in JavaScript?

Trigger Click Event in JavaScript Using click() An element receives the click event when pressed, and a key is released on the pointing device (eg, the left mouse button) while the pointer is within the element. click() is triggered after the down and up mouse events are triggered in that order.

What is the difference between click and Onclick?

click is a function on HTML elements you can call to trigger their click handlers: element. click(); onclick is a property that reflects the onclick attribute and allows you to attach a "DOM0" handler to the element for when clicks occur: element.


2 Answers

In jQuery events are triggered strictly in the order in which they were registered.

You can circumvent any inline DOM0 onclick style handlers by iterating over the whole DOM, removing those onclick properties, and then registering your own handler which then invokes the originally defined function.

Something like:

$('[onclick]').each(function() {
    var handler = $(this).prop('onclick');
    $(this).removeProp('onclick');
    $(this).click(handler);
});

See http://jsfiddle.net/alnitak/2SCTK/

So, register your own handlers first with jQuery, then invoke the code above to remove the original inline onclick handlers and re-register them as if jQuery had added them.

EDIT code simplified to just use the original function - jQuery will ensure that the function is invoked with the right context. Previous code which explicitly set the context to window was incorrect.

like image 133
Alnitak Avatar answered Sep 22 '22 08:09

Alnitak


You can remove the onClick event from the DOM element, and then rebind it using jQuery.

<div id="click" onclick="alert('DOM')">CLICK ME</div>

$('#click').click(function(){
    alert('jQuery');
});

var clickFunc = $('#click').prop('onclick');
$('#click').removeProp('onclick');
$('#click').click(clickFunc);

Events are called in the order they are bound, so the only way to call your function first, is to unbind, and then rebind the original function.

Demo: http://jsfiddle.net/wYd5t/2/

like image 41
Rocket Hazmat Avatar answered Sep 22 '22 08:09

Rocket Hazmat