Update per comments
@adeneo: Armed with answers from this question about syntax for disabled
attribute and this question for custom attributes, I felt content adding that attribute to elements other than the ones listed in the docs as ones that can be "actually disabled". So indeed some of my <a>
and even <span>
elements may obtain that attribute, for more unified way to address them with CSS and JS.
In an effort to be concise and future-proof (e.g. somebody adds an input or two to the page later on), I attach my click event listeners to all the similar elements at once - and handle them according to what the element is. This question was prompted by necessity to disable some elements until ajax request completes. The call gets triggered with click, and I want to prevent additional calls before the first one completes.
@Patrick Q I don't know how to convey feasibility of my intention with a really short code snippet - so I wrote an example in codepen:
example on codepen.
original question:
I would like to add a condition to my "click" event listeners to check if target element is disabled.
Some of my links have or obtain "disabled" attribute (for a number of reasons using links is more convenient - although the interaction is handled with js). In this case, I would like the click
handler to just return false (i.e. preventDefault and stopPropogation) right away, so disabled link would indeed be disabled.
I could of course search for all click handler attachments in my code and add the condition in each individually - but that's really really un-DRY. Another way would be to bind/unbind the events based on the attribute - but that seems just way messy as well... I'd like to change it in one place so it just works everywhere :-).
Per jQuery docs, click
for attaching listeners is just a shorthand for $.on()
. So I imagine I could extend functionality of that... But HOW?
Is it even the right direction to think in?