0

I have a jQuery plugin (which I've written) which has an option to match an optional child selector. Ie: If I have this markup:

<div class="header">
  <a id="item-name">Blah</a>
  <span class="some-data">Numbers and Stuff</a>
  <span class="other-data">Bananas</a>
</div>

Orignally, I delegated the click event to the whole .header using:

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

But then needed to limit the click to only the anchor, so to maintain compatibility, I used the alternative syntax:

headerDiv.on('click',headerSelector, function () {

Where headerSelector, by default is *, but I could pass a to limit it to just the anchor. The default * selector I'm now using seems problematic though, sometimes clicks aren't registered. Is there a proper way to match the element itself when using the delegated event syntax?

Ashish Bahl
  • 1,482
  • 1
  • 18
  • 27
Echilon
  • 10,064
  • 33
  • 131
  • 217

2 Answers2

0

By default you can do

headerSelector = headerDiv

I think it will be equal to headerDiv.on('click', function () {

naortor
  • 2,019
  • 12
  • 26
0

Use document as the parent element and inside selector use headerDiv and then a *. if child is given replace * with it.

$(document).on('click', "headerDiv *", function(){})
Monzurul Shimul
  • 8,132
  • 2
  • 28
  • 42