a few days ago I wasted a lot of time and raw nerves on a tiny problem with legacy code.
The project in question uses jQuery extensively, including its event system. On a page I wanted to prevent the default action on a link tag.
in the page.
Yay, all good now … or not. The form still submitted!
After some time banging my head against the wall, looking for some other events that might fire, I finally saw and realized, that the form submit is attached to the link tag via an inline evend handler.
While looking through the jQuery events attached to the tag, I didn’t see this event. By the way, small hint:
If you want to see all events attached to a jQuery selector, the fastest way is:
That gives you an object with all events,
gives you just the click events.
If you really wand to get rid of all events bound to a html element, jQuery or inline, you have to unbind via jQuery and remove the content of the onclick attribute.
The better way would be of cource to get rid of those inline event handlers altogether.
Finally, a full working example of the problem I just described:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
Click a few times on the link and watch the dev console. The first time, you’ll see both log calls, after that only the one from the inline event. The jQuery event gets removed, the inline event stays.