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
What Is User Datagram Protocol (UDP)? | Fortinet
The best private search engines in 2024 - Surfshark
Somboun Asian Market
Urist Mcenforcer
Ffxiv Shelfeye Reaver
Craftsman M230 Lawn Mower Oil Change
Wisconsin Women's Volleyball Team Leaked Pictures
Top Financial Advisors in the U.S.
Erskine Plus Portal
Corpse Bride Soap2Day
Optum Medicare Support
Pbr Wisconsin Baseball
13 The Musical Common Sense Media
Gt Transfer Equivalency
454 Cu In Liters
Turning the System On or Off
7 Low-Carb Foods That Fill You Up - Keto Tips
Pricelinerewardsvisa Com Activate
Kamzz Llc
FDA Approves Arcutis’ ZORYVE® (roflumilast) Topical Foam, 0.3% for the Treatment of Seborrheic Dermatitis in Individuals Aged 9 Years and Older - Arcutis Biotherapeutics
Finalize Teams Yahoo Fantasy Football
Japanese Mushrooms: 10 Popular Varieties and Simple Recipes - Japan Travel Guide MATCHA
Zillow Group Stock Price | ZG Stock Quote, News, and History | Markets Insider
At&T Outage Today 2022 Map
Jordan Poyer Wiki
kvoa.com | News 4 Tucson
Cornedbeefapproved
Sinai Sdn 2023
How Do Netspend Cards Work?
Kelley Fliehler Wikipedia
Otis Offender Michigan
Stolen Touches Neva Altaj Read Online Free
Www Craigslist Com Shreveport Louisiana
How to Watch the X Trilogy Starring Mia Goth in Chronological Order
Arcadia Lesson Plan | Day 4: Crossword Puzzle | GradeSaver
Tds Wifi Outage
Elgin Il Building Department
Hindilinks4U Bollywood Action Movies
Temu Y2K
Craigslist Tulsa Ok Farm And Garden
Cranston Sewer Tax
Barstool Sports Gif
412Doctors
Timothy Warren Cobb Obituary
Professors Helpers Abbreviation
Dontrell Nelson - 2016 - Football - University of Memphis Athletics
Copd Active Learning Template
Bonecrusher Upgrade Rs3
The 13 best home gym equipment and machines of 2023
Kidcheck Login
Arnold Swansinger Family
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.