Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

jquery document on click with ":not" not working

I am sorry if the title is terrible, I was really trying to figure out how to word this lol

What I am trying to do is I have links that a user will click on, within the <a> I have a class called not_me when the user clicks on that I don’t want to fire the event. I tried to use :not(.not_me) but the click event is still firing. What am I missing?

jQuery

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

jQuery(document).on("click",".co_link:not(.not_me)", function() {

})

HTML

<a class="co_link"><span>Here is some other stuff </span><span class="not_me">Click Me</span></a>

>Solution :

The event target of the .not_me element does not match the .co_link selector. You’re actually targeting an element like: <a class="co_link not_me">.

You will need to stop propagation on the .not_me element first:

$(document).on('click', '.not_me', (e) => { e.stopImmediatePropagation(); });
$(document).on('click', '.co_link', (e) => { alert('clicked'); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<a class="co_link"><span>Here is some other stuff </span><span class="not_me">Click Me</span></a>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading