Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add event listener to all objects except for a few selected?

I'm trying to add event listener to all objects except for a few selected(The selected also have arbitrary child elements in arbitrary levels)?

I have asked this question before, but I didn't really got an answer to it. I have came close to solve it. Could you please help me with debugging it?

I'm adding an event listener to the body element called bodylistener and an event listener to the few selected elements called selectedElementsMarkTrue. The few selected elements that I don't want to execute some code, the listener selectedElementsMarkTrue performs pior to bodylistener with setTimeout function. selectedElementsMarkTrueset the variable selectedElements to true and bodylistenerchecks if selectedElementsis truebefore execute som code. There is still something wrong with my code

    var selectedElements = false;
    var bodylistener = function(){
        window.setTimeout(function(){//Setting timeout so that the other handler, selectedElementsMarkTrue, always performs first
            (function(){
                if(selectedElements == false){
                    //Do some stuff
                }else{
                    selectedElements = false;
                }
            });
        }, 10);//Could be 0, but te be sure I set 10
    };
    var selectedElementsMarkTrue = function(){
        selectedElements = true;
    };
    $('#dontAddEventListener1, #dontAddEventListener2').each(function(){
        this.addEventListener('click', selectedElementsMarkTrue, false);
    });
    document.body.addEventListener('click', bodylistener, false);

I can't get the setTimeout function to execute the underlying code?

like image 878
einstein Avatar asked Apr 12 '11 09:04

einstein


1 Answers

Sounds like you want behavior something like this:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <script>
            window.addEventListener("DOMContentLoaded", function() {
                document.body.addEventListener("click", function(e) {
                    var el = e.target;
                    do {
                        if (el.hasAttribute && el.hasAttribute("data-nofire")) {
                            return;
                        }
                    } while (el = el.parentNode);
                    alert('do stuff');
                }, true);
            }, false);
        </script>
    </head>
    <body>
        <p><span>click me</span></p>
        <p data-nofire><span>click me</span></p>
        <p data-nofire>click me</p>
        <p>click me</p>
    </body>
</html>

Or, you could do it something like Naren suggested:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title></title>
        <script>
            window.addEventListener("DOMContentLoaded", function() {
                var nofire = document.getElementsByClassName("nofire");
                for (var i = 0; i < nofire.length; ++i) {
                    nofire[i].addEventListener("click", function(e) {
                        e.stopPropagation();
                    }, true);
                }
                document.body.addEventListener("click", function(e) {
                    alert('do stuff');
                }, false);
            }, false);
        </script>
    </head>
    <body>
        <p><span>click me</span></p>
        <p class="nofire"><span>click me</span></p>
        <p class="nofire">click me</p>
        <p>click me</p>
    </body>
</html>
like image 53
Shadow2531 Avatar answered Oct 16 '22 02:10

Shadow2531