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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:56:45+00:00 2026-06-18T04:56:45+00:00

I would emulate in pure Javascript the main functionality of jQuery .on( events ,

  • 0

I would emulate in pure Javascript the main functionality of jQuery .on( events , selector , data) method.

For example

$(document).on('click','.button',function() {
   console.log("jquery onclick"); 
});

I thought it was enough make something like this

document.addEventListener('click',function(e) {
    if(e.target.className == 'button2') {
         console.log("It works");   
    }
});

However when I have this html structure:

<button class="button2">Hello <span>World</span></button>

my script doesn’t works when the click event is triggered on span element, because e.target is span. (I ignore for this question the complexity of elements with multiple class, and crossbrowsers compatibility)

The source of jQuery is not simple to read and I don’t understand how it works (because the first piece of code, in jQuery, works with my html structure).

I need this method because my html is dynamic, and buttons with this class are created, deleted and re-created many times. I don’t want add listeners every times.

I would avoid, if possible, to include jquery library.

So, I can do this?

Here the jsFiddle for testing.

  • 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-18T04:56:46+00:00Added an answer on June 18, 2026 at 4:56 am

    This is actually surprisingly simple. You’re on the right track, but it’s not quite there.

    Here’s the functions I use:

    window.addEvent = function(elem,type,callback) {
        var evt = function(e) {
            e = e || window.event;
            return callback.call(elem,e);
        }, cb = function(e) {return evt(e);};
        if( elem.addEventListener) {
            elem.addEventListener(type,cb,false);
        }
        else if( elem.attachEvent) {
            elem.attachEvent("on"+type,cb);
        }
        return elem;
    };
    window.findParent = function(child,filter,root) {
        do {
            if( filter(child)) return child;
            if( root && child == root) return false;
        } while(child = child.parentNode);
        return false;
    };
    window.hasClass = function(elem,cls) {
        if( !('className' in elem)) return;
        return !!elem.className.match(new RegExp("\\b"+cls+"\\b"));
    };
    

    The window.findParent is central to the whole thing, as you can see when I show you how to attach your desired on listener:

    window.addEvent(document.body,"click",function(e) {
        var s = window.findParent(e.srcElement || e.target,function(elm) {
            return window.hasClass(elm,"button");
        },this);
        if( s) {
            console.log("It works!");
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to emulate click events in Swing using following code: event =
I need to have functionality that would essentially emulate user creating a port forwrding
I want to emulate the functionality of gzcat | tail -n. This would be
I would like to be able to emulate the functionality of the Presenter Tools
Is there any open source collection framework, written in ActionScript that would emulate HashMap,
How would you emulate touch-screens? I wouldn't want to pay $ to get a
I would like to emulate the following interface: interface MultiSideEffectFunction<T> { void action(T first,
I would like to emulate the pass-by-value behaviour in python. In other words, I
I am trying to emulate a system of type classes in F#; I would
I would like to emulate the 'Drop Pin' feature in the Maps application. I

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.