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 8823205
In Process

The Archive Base Latest Questions

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

I’m looking for the most brilliant idea. The focus is Efficiency – let’s try

  • 0

I’m looking for the most brilliant idea. The focus is Efficiency – let’s try to avoid a bunch of loops =)

No libraries. This is an exercise in pure JS genius. Feel free to reference functions from inside a library though, if they accomplish this. No need for fallbacks to crappy old browsers (ie: IE).

The Setup

I am trying to create event delegation. I am binding an event listener to a container element (the context) for each event type that happens within this element (click, mouseenter, etc.). The listener references a function, we’ll call the “route”.

There’s an object, created separately that looks something like this (example):

myarr['click']['.myclass'] = functionReference1;
myarr['click']['#myid'] = functionReference2;
myarr['click']['div>a'] = functionReference3;

The route takes event.type (here, ‘click’) and goes to myarr and gets all the click records. route then loops through each selector key and matches it against the event.target. If it matches, the function reference is called, the callback if you will.

This is all well and good. Fairly quick, straightforward.

The Question

The event.target may be a child of an element that matches the selector key. I’m looking for the crazy smooth voodoo black magic to determine if that parent is between our context element and event.target. Preferably, some native browser function.

IDEALLY, as example, I could do something along the lines of

context.querySelectorAll(key + " " + event.target);

but obviously, I can’t pass an object into querySelectorAll, as far as I know.

What’s Been Tried

Two methods have been tried. Both WORK. Neither is pretty.

1)

Loop through the event.target.parentNode until a match is found. This totes does it, but it creates a loop inside my existing loop. Starts getting SLOW when there’s several selector keys and lots of elements on the page.

if (event['target'].matchesSelector(key)) {
    //do callback
} else {
    var et = event['target'];
    while (et['parentNode'] !== null and et['parentNode'] !== this) {
        et = et['parentNode'];
        if (et.matchesSelector(key)) {
            //do callback
            break;
        }
    }
}

2)

Use ranges. I’m rather proud of this one, but it has the same multi-loop problem. Haven’t tested performance too heavily, but there’s MANY things that happen here, and if there’s a LOT of elements matching the selector key, this is probably going to hurt efficiency.

if (event['target'].matchesSelector(key)){
    //do callback
} else {
    var range2 = document.createRange();
    range2.selectNode(event.target);
    var range = document.createRange();         
    var allthese = document.querySelectorAll(key)
    for (var i = 0; i < allthese.length; i++){
        range.selectNode(allthese[i]);
        if (range2.compareBoundaryPoints(range2.START_TO_START, range) && range2.compareBoundaryPoints(range2.END_TO_END, range) <= 0){
            //do callback
            break;
        }
    }
}

MAGIC VOODOO – find me some, and I’ll create a big bounty when I have the rep again.

  • 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-14T06:17:42+00:00Added an answer on June 14, 2026 at 6:17 am

    Maybe you should try this:

    event['target'].matchesSelector(key) || event['target'].matchesSelector(key + ' *')
    

    Here my second condition will check if the target is Descendant of the key selector

    As you can see in this JSFIDDLE(fiddle Works in webkit(prefer chrome) browsers only) the child/Descendant is matched using parent selector

    PS: This method would not work if you also need that element which matches the actual key, but in your question as you said “crazy smooth voodoo black magic to determine if that parent is between our context element and event.target”, so I suppose you don’t need the actual element

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
This could be a duplicate question, but I have no idea what search terms
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.