Unable to select anything expect #element

I want jQuery to respond to a click on any element except <img src=“path.jpg” alt=“” id=“test”>

this code works, but only if I’m clicking on images:


I really want this but it doesn’t work:


What can I do so jQuery will detect a click on any element in the page except #test?

An easier way of going about this is by binding a click event to the document instead of determining if the target element matches anything but #test.

$(document).on('click', function(e) {
    if (e.target.id !== 'test') {

Thanks Chris!

I like your solution better!

I found one online that uses 2 queries:

$('#test').on('click', function(e) {


$('document').on('click', function(e) {

    // respond to any non #test click


I thought I understood event propagation, but I’m not sure how stopping it on a non #test click works…

I think I’ll use your way because I have more than 1 situation to handle on different element in anything but it is clicked.

The example you posted shows how you can make your code more abstract but you would only use that in the case that #test has something special action attached to it, when it comes down to it there are different ways to write the same snippet of code but it all depends on what the click event is doing.