a:not(a:not([href])) selector

Viewed 13760

I want that a certain action to be associated to the click event of the anchor tag whenever its href attribute:

  • does not start with mailto: and
  • does not end with # and
  • is present with any value including empty

So I was trying this code:

<a href="example.com">example.com</a>
<a href="mailto:someone@www.example.com">Someone</a>
<a href="thepage#">The Page</a>
<a>This does nothing</a>

<script type="text/javascript">
    $(document).on('click', 'a:not([href^="mailto\\:"], [href$="\\#"], [href])', myFunction);
</script>

And it didn't work. But to my dismay this works:

$(document).on('click', 'a:not([href^="mailto\\:"], [href$="\\#"], a:not([href]))', myFunction);

But I don't understand how. Notice the inner :not(). As I understand the [href] means there is no href attribute. Or is it the opposite?

Could someone lead me to the light?

1 Answers
Related