avatarJohn Au-Yeung

Summary

The provided web content explains how to attach a click event listener to all elements on a page except for one specific div using jQuery.

Abstract

The article titled "How to Add a Click Listener for Any Element in the Page Except on 1 Div with jQuery?" details a method for implementing a click event listener that applies to all page elements except a specified div. The author illustrates this by using a jQuery function that adds a click event to the body, then checks if the clicked element's ID matches the one to be excluded. If the ID matches, the function exits early; otherwise, it proceeds to log a message to the console. This approach ensures that the event listener behaves as intended without triggering on the excluded div. The article concludes by reiterating the method and encourages engagement with the "In Plain English" community through various social platforms and newsletters.

Opinions

  • The author provides a practical solution to a common problem faced by web developers working with jQuery and event handling.
  • The technique of checking the event target's ID is presented as an efficient way to exclude specific elements from event listeners.
  • The article promotes the "In Plain English" community, suggesting that clear communication and community engagement are valued.
  • The inclusion of social media links and other platforms indicates a strategic effort to grow the community and increase content reach.

How to Add a Click Listener for Any Element in the Page Except on 1 Div with jQuery?

Photo by Tawhidur R on Unsplash

Sometimes, we want to add a click listener for any element in the page except on 1 div with jQuery.

In this article, we’ll look at how to add a click listener for any element in the page except on 1 div with jQuery.

Add a Click Listener for Any Element in the Page Except on 1 Div with jQuery

To add a click listener for any element in the page except on 1 div with jQuery, we can add a click event listener for the body and then check which element is clicked in the click event listener.

For instance, if we have:

<div id='menu-content'>
  menu
</div>
<div>
  foo
</div>

Then we can add the click event listener to the body by writing:

$('body').click((evt) => {
  if (evt.target.id === "menu-content") {
    return
  }
  console.log('clicked')
});

We call click with a callback that takes the evt event object.

Then we check if evt.target.id is 'menu-content' .

If it is, we stop running the function with the return statement.

Otherwise, we log 'clicked' .

Therefore, when we click on ‘foo’, we see 'clicked' logged.

Otherwise, we see nothing in the console log.

Conclusion

To add a click listener for any element in the page except on 1 div with jQuery, we can add a click event listener for the body and then check which element is clicked in the click event listener.

In Plain English 🚀

Thank you for being a part of the In Plain English community! Before you go:

Programming
Web Development
Technology
Software Development
JavaScript
Recommended from ReadMedium