Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7713617
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T01:55:02+00:00 2026-06-01T01:55:02+00:00

When delegating events using .on how do I target child elements: I have tried:

  • 0

When delegating events using .on how do I target child elements:

I have tried: childSelector =

  • >*
  • >:nth-child(n)

But nothing is selected when I start with >.

$(selector).on(event, childSelector, handler);

Sometimes I want to target a direct child, sometimes I don’t: (pseudo code)

var func = function(selector, subSelector) {
    $(selector).on("click", subSelector, function() {
        alert("my subSelector is clicked");
    });
}

func("#wrapper", "direct-child-selector");
func("#wrapper", ".some .other > .selector:first");

Thats why I’m asking for a selector and not a fix.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T01:55:03+00:00Added an answer on June 1, 2026 at 1:55 am

    You could check within the handler whether the element is a child of the element the event handler was delegated to;

    $(selector).on("event", '*', function (e) {
        if (e.target.parentNode === e.delegateTarget) {
            // Woo!
        }
    });
    

    See e.delegateTarget. Its worthy to note that e.delegateTarget was introduced in jQuery 1.7, so won’t work on older versions.

    In regards to your second edit, in it’s current form the selectors are ambiguous; there’s no way for you to, in code and in its current form, detect whether the selector passed is intended to be a child only selector. You can either introduce another parameter to indicate whether it’s intended to be a child only selector, or add a > to the start of the selector (e.g.) and check for that;

    var func = function(selector, subSelector, isChild) {
        $(selector).on("click", subSelector, function(e) {
            if (isChild && e.parentNode == e.delegateTarget || !isChild) {
                 alert("my subSelector is clicked");
            }
        });
    }
    
    func("#wrapper", "direct-child-selector", true);
    func("#wrapper", ".some .other > .selector:first" /* , `false` is optional */);
    

    Or:

    var func = function(selector, subSelector) {
        if (subSelector.charAt(0) === '>') {
            subSelector = selector + subSelector;
        }
    
        $(selector).on("click", subSelector, function(e) {
            alert("my subSelector is clicked");
        });
    }
    
    func("#wrapper", "> direct-child-selector");
    func("#wrapper", ".some .other > .selector:first");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Say I have two tables (I am using Django, but this question is mostly
I have a system that manages events that happen to multiple actors using a
Event-delegation must be used to make events work after inserting DOM elements (i.e Ajax
I am using a networking library which uses a thread pool for delegating tasks
Is it possible to have event delegation using the HTML5 data attributes in MooTools?
I have a div <div class=myDiv> <a href=# class=myLink>somelink</a> <div class=anotherDiv>somediv</div> </div> Now, using
I have an InstallShield (InstallScript) installation, created using IS2009, that's built automatically every night,
I encountered an issue using jQuery's .delegate() method on IE8 when removing the elements
(Note: I'm using jQuery below, but the question is really a general JavaScript one.)
I have a custom developed WordPress plugin that is using jQuery 1.4 and for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.