How to detect which element is clicked using jQuery

Image contains code to detect which element is clicked using jQuery

Imagine that you have a webpage that has a lot of elements that you want to access, but you don’t want to create an ID for each one and use a different instruction to access them. For that, you can use the power of jQuery to detect which element is clicked with a few lines of code. The event is linked to the element action, for this you can use the event in the function to get the element.

This blog post provides the information needed to detect which element is clicked using jQuery.

Suppose you have the code below in your html page and you want to detect which button you clicked.

<div class="col-md-12 text-center mb-1">
            <h3>Make a Gift</h3>
                <button type="button" class="btn btn-primary">$10</button>
                <button type="button" class="btn btn-primary">$25</button>
                <button type="button" class="btn btn-primary">$60</button>
                <button type="button" class="btn btn-primary">$160</button>
                <button type="button" class="btn btn-primary">$400</button>

First of all, you must have the jQuery library and it must be placed before your jQuery code. Here is the way to add the jQuery library:

<script src=""></script>

Below is the jQuery code to detect the button you clicked.


Another way to detect the element you clicked is to use the Target method. Below is the jQuery code to detect which h3 you clicked on.


Now to remove the button you clicked, use the jQuery remove method to do so.


Video to show you how to remove the button you clicked using jQuery

Note: You can find a copy of the full code I used in this blog post on my GitHub page (link is in my bookmark page).

What To Read Next

If you like the content of this post or if it has been useful to you, please consider sharing it on your social media and follow me on Facebook and Twitter for more exclusive content.

Share or Like to
unlock the Page

Or wait 03:00 minutes

Subscribe To Our Newsletter

Join our list of subscribers to get the latest update, free tips, tutorials, and checklist to resolve your technical issues.

  • Discounts
  • Latest Updates
  • Checklist
No thanks. I don't want to learn anymore

Don't forget to Subscribe

Your Information will never be shared