Zanderwar Zanderwar - 2 years ago 85
Javascript Question

Determine clicked child whilst it's parent has it's own click listener

I have multiple accordion widgets on page.

To elaborate on how it's all setup

  • The green heading as shown in the image below, is the handle for jQuery UI sortable

  • The green heading also has a click listener which collapses/expands the accordion.

  • The edit button has it's own click listener which when fired does as intended but will also fire the listener to collapse the accordion

Is it possible to get the ACTUAL element that was clicked so that I can check that it was not the edit/save/discard etc buttons?

My accordion JS is:

this.accordion = function () {
var element = $('.accordion > .panel-heading');

element.on('click', function () {
var self = $(this);
if (self.hasClass('open')) {


My obviously incomplete "Edit" JS is:

this.editableWidgets = function () {

var edit = $('.panel .widgetBtn_edit');
var save = $('.panel .widgetBtn_save');
var discard = $('.panel .widgetBtn_discard');

edit.on('click', function () {

save.on('click', function () {
alert('hook up saving functionality')

discard.on('click', function () {


Fig 1

Before = Before clicking Edit

After = After clicking Edit

Figure 1

Answer Source

In order to get the actual element that was clicked you can use the event's target property.

In the following example try clicking on the bordered div and on the green span and see the different ids alerted:

//Event listener on outer element (#outer)
$(document).on('click', '#outer', function(e) {
  // gets the actual element clicked
  var trg =;
  var id = $(trg).attr('id');
#outer {
  border: 1px solid #d8d8d8;

#inner { background-color:green; color:#fff }
<script src=""></script>
<div id="outer">
  <span id="inner">Inner</span>

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download