I want all the clicks on links to execute a certain action with the exception of mailto links. I have the following code that works as expected. It first attaches the function to all the links and then removes it from the mailto links:
<a href="http://www.example.com">example.com</a>
<a href="mailto:someone@www.example.com">Someone</a>
<script type="text/javascript">
$('a').on('click', function () {
theAction();
});
$('a[href^="mailto"]').off('click');
</script>
I would like the same functionality but using the :not() selector as it looks more elegant. How would it be?
Rather than attaching the event to each and every link, delegate it. This also makes it pretty easy to filter which do and don’t fire it.