Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why I can't click on this div?

Tags:

html

jquery

css

When I click on the x inside of the close div, I want the background to change to white.

This is the markup:

<div class="list-item list-item-active">
    <div class="close">x</div>
</div>

This is the javascript:

$(document).ready(function(){
    $('.list-item').live('click', function() {
        if (!$(this).hasClass('list-item-active'))
            $(this).addClass('list-item-active');
    });
    $('.list-item .close').live('click', function() {
        $(this).parent().removeClass('list-item-active');
    });
});

This is the css:

.list-item {width:100px;height:100px;background:#fff}
.list-item-active {background:#ccc}

Demo: http://jsfiddle.net/JMeff/

like image 516
Vytautas Avatar asked Dec 13 '22 17:12

Vytautas


1 Answers

You can click it, but the click also clicks the parent due to default event bubbling. To get the effect you want, stop the bubble via .stopPropagation(), like this:

$('.list-item .close').live('click', function(e) {
    $(this).parent().removeClass('list-item-active');
    e.stopPropagation();
});

You can test it out here.

like image 121
Nick Craver Avatar answered Dec 31 '22 02:12

Nick Craver