Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Click event not triggering CSS pointer-events: none

Tags:

jquery

css

I have two images stacked one above the other. img_top has 'pointer-events: none' set, so clicking on it will pass the click to img_bottom. However if I invoke jQuery's trigger('click') on img_top, the click will not pass on to img_bottom.

Can anybody explain why and find a way of passing the click to the bottom image using jQuery's trigger('click') AND CSS's pointer-events?

Here is the code I am using:

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<style type="text/css">
    #img_holder {
        position: relative;
        width: 20px;
        height: 20px;
    }

    #img_bottom {
        position: absolute;
        background: url('/images/cross.png');
        width: 16px;
        height: 16px;
        z-index: 1;
    }

    #img_top {
        pointer-events: none;
        position: absolute;
        background: url('/images/tick.png');
        width: 16px;
        height: 16px;
        z-index: 2;
    }
</style>

<script type="text/javascript">
    $(document).ready(function() {
        $(document).click(function() {
            alert('clicked');
            $("img_top").trigger('click');
        });

        $("#img_bottom").click(function() {
            alert('success');
        });
    });
</script>

<div id="img_holder">
    <div id="img_top"></div>
    <div id="img_bottom"></div>
</div>
like image 279
Adrien Hingert Avatar asked Mar 08 '26 16:03

Adrien Hingert


1 Answers

This is because in javascript events bubble up the DOM. img_bttom is a sibling of img_top, so the event would never bubble to it.

You can force it manually though:

$("#img_top").click(function(event) {
    $("#img_botton").trigger(event);
})
like image 98
Rory McCrossan Avatar answered Mar 10 '26 06:03

Rory McCrossan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!