How To Detect Which Element Was Clicked, Using jQuery (2024)

Metal Toad is an AWS Managed Services provider. In addition to jQuery help we recommend checking out our article on how to host a website on AWS in 5 minutes.

Sometimes we would like to find out which particular element (or set of elements) has user clicked on. However, binding click events on each element manually might not be the most practical way of accomplishing this. Or we might want to fire a certain event if user clicks on anything but particular element(s). I will describe one of such scenarios below and offer one of possible solutions to it using jQuery.

Example

In our example we have a menu toggle button which shows/hides menu item list. We want the item list to stay open until user clicks on the menu toggle button or somewhere outside the item list.

How To Detect Which Element Was Clicked, Using jQuery (1)

<!DOCTYPE html><html><head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <title>Menu Toggle</title> <style> .item-list { display: none; } .show-list { display: block; } </style></head><body> <div class="main"> <header> <div class="toggle"></div> <ul class="item-list"> <li id="item-1"></li> <li id="item-2"></li> <li id="item-3"></li> <li id="item-4"></li> </ul> </header> </div></body></html>

Implementing Toggle Button

We have a basic layout with menu list being hidden by default. We have a CSS class .show-list which we can apply to the item list to override display: none; and show the list to the user. Now we need to bind the functionality to .toggle button to add/remove .show-list class to menu list. We can easily do this using jQuery:


// $('.toggle').on('click', function() {});
// is equivalent of $('.toggle').click(function() {});
$('.toggle').on('click', function() {
$('.item-list').toggleClass('show-list');
});

Detecting Out-of-Bounds Clicks

Great, we have toggle button functionality! All we need to do now is figure out a way to detect clicks outside of the menu list and hide the list when out of bounds click is detected. Again, jQuery makes this task an easy one.


// Bind click event listener on the body
// Hide main menu if user clicks anywhere off of the menu itself.
$('body').on('click.hideMenu', function(e) {
// Check to see if the list is currently displayed.
if ($('.item-list').hasClass('show-list')) {
// If element clicked on is NOT one of the menu list items,
// hide the menu list.
if (!$(e.target).parent().hasClass('item-list')) {
$('.item-list').removeClass('show-list');
}
}
});

Code Breakdown

$('body').on('click.hideMenu', callbackFunction);

does the same thing as

$('body').on('click', callbackFunction);

jQuery allows us to namespace event handlers. This way if someone unbinds 'click' event on the body, it will not break our hideMenu handler. If we want to remove this we have to unbind 'click.hideMenu' handler directly.

$(e.target) - Contains DOM element user clicked on.

We can use this object to get anything and everything regarding object user has clicked on. In the code above, I'm checking for parent class of the clicked element. If the parent class is .item-list, then user clicked on one of the menu items.

NOTE: e.target is always going to be the top element clicked. In our scenario, child element is always going to be on top of the parent element. So while every click on the screen is technically click on the body, e.target will always return the furthest child element in the tree that occupies clicked area. However, we might have multiple absolutely positioned elements occupying the same area. In this case, the element with higher z-index will be returned.

How To Detect Which Element Was Clicked, Using jQuery (2024)
Top Articles
Trade Settlement - Definition, Types, Settlement in NSE/BSE
Norway Is Running Out of Gas-Guzzling Cars to Tax
Www.fresno.courts.ca.gov
Skamania Lodge Groupon
Napa Autocare Locator
Fototour verlassener Fliegerhorst Schönwald [Lost Place Brandenburg]
Hay day: Top 6 tips, tricks, and cheats to save cash and grow your farm fast!
Best Theia Builds (Talent | Skill Order | Pairing + Pets) In Call of Dragons - AllClash
Vocabulario A Level 2 Pp 36 40 Answers Key
When Is the Best Time To Buy an RV?
Bbc 5Live Schedule
Goldsboro Daily News Obituaries
Healing Guide Dragonflight 10.2.7 Wow Warring Dueling Guide
I Wanna Dance with Somebody : séances à Paris et en Île-de-France - L'Officiel des spectacles
سریال رویای شیرین جوانی قسمت 338
House Of Budz Michigan
Uktulut Pier Ritual Site
Panic! At The Disco - Spotify Top Songs
Att.com/Myatt.
Self-Service ATMs: Accessibility, Limits, & Features
Craigslist Illinois Springfield
Makemv Splunk
WRMJ.COM
Intel K vs KF vs F CPUs: What's the Difference?
Our Leadership
Barbie Showtimes Near Lucas Cinemas Albertville
R/Sandiego
Duke Energy Anderson Operations Center
Otis Offender Michigan
Panchang 2022 Usa
Sun-Tattler from Hollywood, Florida
Fridley Tsa Precheck
Envy Nails Snoqualmie
Yoshidakins
Rise Meadville Reviews
Foolproof Module 6 Test Answers
That1Iggirl Mega
Myql Loan Login
Planet Fitness Santa Clarita Photos
craigslist | michigan
MSD Animal Health Hub: Nobivac® Rabies Q & A
Lake Kingdom Moon 31
Doublelist Paducah Ky
Sofia Franklyn Leaks
Haunted Mansion (2023) | Rotten Tomatoes
R/Gnv
Dancing Bear - House Party! ID ? Brunette in hardcore action
Bank Of America Appointments Near Me
Dlnet Deltanet
Latina Webcam Lesbian
Strange World Showtimes Near Atlas Cinemas Great Lakes Stadium 16
Access One Ummc
Latest Posts
Article information

Author: Dr. Pierre Goyette

Last Updated:

Views: 6452

Rating: 5 / 5 (50 voted)

Reviews: 89% of readers found this page helpful

Author information

Name: Dr. Pierre Goyette

Birthday: 1998-01-29

Address: Apt. 611 3357 Yong Plain, West Audra, IL 70053

Phone: +5819954278378

Job: Construction Director

Hobby: Embroidery, Creative writing, Shopping, Driving, Stand-up comedy, Coffee roasting, Scrapbooking

Introduction: My name is Dr. Pierre Goyette, I am a enchanting, powerful, jolly, rich, graceful, colorful, zany person who loves writing and wants to share my knowledge and understanding with you.