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

  • Home
  • SEARCH
  • 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 9150961
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:41:07+00:00 2026-06-17T11:41:07+00:00

is there any way to use a jQuery selector in a delegated event with

  • 0

is there any way to use a jQuery selector in a delegated event with .on() ?
And how would you select only immediate children in the selector ?
Below is a sample of code : I’d like to filter only immediate <div> children of the <section> element, with the requirement that at least one of their <p> children contains the word «hello».
In the example, only the second <div> would be filtered. The problem is that other <div> can be added afterwards, so the event must be delegated.

The use of the .live() method made it more simple imho, as I could have used :

$('section > div').filter(function(){return /hello/.test(p) }).live('mouseenter', function(){ ... })

But as it’s deprecated now, its replacement .on() only allows pure CSS-like selectors in delegated events.
Has anyone got any idea on how to filter these elements based on the 2 aforementioned conditions (immediate children & <p> contain hello) ?
Thanks

<section>
    <div>
        <p>abc</p>
        <div>
            <p>def</p>
            <p>hello</p>
        </div>
    </div>
    <div>
        <p>hello world</p>
        <p>test</p>
    </div>
</section>

EDIT : I forgot to add my JS sample, and I’m modifying the condition a little bit so that p:contains(‘hello’) does not suffice as a selector.

$('section').on({
    mouseenter: function(){
        $(this).css('background-color','red');
    }
},
    $('div').filter(function(){
        var p = $(this).children('p').filter(function(){
            return /hello/.test($(this).text());
        });
        return p.length > 2;
    })
);
  • 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-17T11:41:08+00:00Added an answer on June 17, 2026 at 11:41 am
    $('section').on('mouseenter', '> div > p:contains("hello")', function(){ ... })
    

    to have the event on the div, you’ll have to set the condition inside the function

      $('section').on('mouseenter', '> div', function(){ 
          if ($(this).find("> p:contains("hello")").get(0)) {
    
             // ...
            }
       })
    

    It was laziness as i always do it in my code : it’s better to attach event to document and delegate; jquery works way way faster that way + you can add element dynamically without worrying of whether event will be triggered or not (it will)

     $(document).on(...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way that I can use JavaScript (and jQuery) to create a
Is there a way I can use a jQuery selector like :empty or :not(:checked)
Is there any way to use both the jquery and scriptaculous js files together?
Is there any way - any jQuery selector (i did found none on http://api.jquery.com/category/selectors/
Is there any way to use Google's API to retrieve a user's current zipcode
Is there any way to use Zend_Filter_Encrypt with large files, without rising memory limit
Is there any way to use global int constants in Objective C that work
Is there any way to use server controls in ASP.NET MVC? Specifically I need
Is there any way to use ActiveRecord calculations while also acquiring relevant field names?
Is there any way to Use the Builtin ScrollBars that comes with each .Net

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.