JavaScript JavaScript Event Delegation - Behavior javascript event delegation example,javascript event delegation and bubbling,javascript

Once you handled the click, you can walk the visual tree, starting from the target, to find the information you need.
You might seek by type (node name), by tag, ... for the sake of the example, i just seek on element up to get a 'data-link' attribute if i don't find one on the target, but you have many choices here.

Edit : another idea is to use event.currentTarget, to get the element on which you hooked the event.

The updated fiddle will print :

delegate call on h1 or p of section 1 

when you click on h1 or p1

and it will print :

delegate section 1 

when you click on the whole section.

http://jsfiddle.net/KMJnA/4/

function delegate(ele) {
    ele.parentNode.addEventListener("click", delegateHandler, false);
}

function delegateHandler (e) {
    var target = e.target;  
    var attribute = target.getAttribute("data-link");
    // if target has no attribute
    // seek it on its parent
    if (!attribute) {
         attribute = target.parentNode.getAttribute("data-link");
    }

    if ( target.classList.contains("myClass") ) { 
         console.log("delegate " + attribute);
        } 
    else console.log('delegate call on h1 or p of ' + attribute);
}    

Rq : i didn't get why you hook the event on the parent of the element, might be simpler not to do it. :-)

Answer:1

I found the solution with CSS pointer-events

jsFiddle with CSS


.myClass * {
    pointer-events: none;
}

or with JavaScript

jsFiddle with JavaScript


(function () {
    "use strict";

    var ele = document.querySelector(".myClass").parentNode;

    delegate(ele);

    function delegate(ele) {
        ele.addEventListener("click", function (e) {
            var target = e.target;

            while (!(target.nodeName.toLowerCase() === "section" && target.classList.contains("myClass"))) {
                target = target.parentNode;
                if (target === ele) {
                    break;
                }
            }

            if (target.nodeName.toLowerCase() === "section" && target.classList.contains("myClass")) {
                console.log("delegate " + target.getAttribute("data-link"));
            }
        });
    }
}());
Answer:2

I am running the Native messaging sample app. (http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extensions/docs/examples/api/nativeMessaging/). I have added registry key ...

I am running the Native messaging sample app. (http://src.chromium.org/viewvc/chrome/trunk/src/chrome/common/extensions/docs/examples/api/nativeMessaging/). I have added registry key ...

  1. chrome native messaging example
  2. chrome native messaging host
  3. chrome native messaging c#
  4. chrome native messaging tutorial
  5. chrome native messaging whitelist
  6. chrome native messaging security
  7. chrome native messaging python
  8. chrome native messaging not running uipath
  9. chrome native messaging
  10. chrome native messaging c# example
  11. chrome native messaging.exe not running
  12. chrome native messaging java
  13. chrome native messaging.exe
  14. chrome native messaging not working
  15. chrome native messaging debug
  16. chrome native messaging manifest
  17. chrome native messaging disconnect
  18. chrome native messaging host example
  19. chrome native messaging host c#
  20. chrome native messaging allowed_origins

I am creating a site using sails and passport for authentication purposes. I've got problems when it come to the use of Jquery and backbone in my code though. It seems that both are down when i tried ...

I am creating a site using sails and passport for authentication purposes. I've got problems when it come to the use of Jquery and backbone in my code though. It seems that both are down when i tried ...

  1. sails backbone

In my HTML file, I want to set a check box with specific name as well as id as= checked. How can I acheive this..? eg: <input type="checkbox" name="myName_1" id="1" value="my Value 1"> my ...

In my HTML file, I want to set a check box with specific name as well as id as= checked. How can I acheive this..? eg: <input type="checkbox" name="myName_1" id="1" value="my Value 1"> my ...

  1. check checkbox with specific value
  2. check specific port windows
  3. check specific port linux
  4. check specific port
  5. check specific port status windows
  6. check specific port open windows
  7. check specific word in string php
  8. check specific commit git
  9. check specific gravity of battery
  10. check specific port cmd
  11. check specific character in string php
  12. check specific character in string javascript
  13. check specific port status linux
  14. check specific port netstat
  15. check specific information
  16. check specific open ports cmd
  17. check specific dns server
  18. check specific port nmap
  19. check specific port open linux
  20. check specific port ubuntu

I created an IndexedDB databse named "A" by indexedDB.open method. Now I want to modify the database name to "B", how can I do it? I dont't want to create a new database with new name and copy all ...

I created an IndexedDB databse named "A" by indexedDB.open method. Now I want to modify the database name to "B", how can I do it? I dont't want to create a new database with new name and copy all ...