Skip to content Skip to sidebar Skip to footer

Check Element Type Clicked

I have a div that I want to expand/contract with the exception of links. I have the following code, which works but lacking the exception. What is the most efficient way of ensur

Solution 1:

If you mean you don't want links to trigger the toggle, use event.target.nodeName:

        $("#expandablediv").click(function (e) {
            if (e.target.nodeName == "A") { return; }
            //if ($(e.target).is('a')) { return; } // also worksif ($("#withinexpandlediv").is(":hidden")) {
                $("#withinexpandlediv").fadeIn(50);
            }
            else {
                $("#withinexpandlediv").fadeOut(50);
            }
        });

nodeName: http://jsfiddle.net/m7vpk/

is(): http://jsfiddle.net/FQuzt/

Solution 2:

Something like

if(!$('#expandablediv').children().has('a')){
    // handle expandsion/contraction here
}

Check this link for more details: jQuery .has()

Solution 3:

you can try this :

$("#expandablediv").click(function () {
            if ($("#withinexpandlediv").is(":hidden")) {
                $("#withinexpandlediv").fadeIn(50);
            }
            else {
                $("#withinexpandlediv").fadeOut(50);
            }
}).find('a').click(function(e){
    e.stopPropagation();
});

Post a Comment for "Check Element Type Clicked"