Jamie
Jamie

Reputation: 431

How to prevent click event to propagate to parent element

Can't really explain it so I made a jsfiddle but as you can see when you open the menu by clicking the toggleButton and you click the div removeBtn and that removes the item but the slide toggle is activated because of it...

I'm not using the conventional click slidetoggle because I'd like the mainMenu to close if the user clicks outside of the div...

Any idea why?

Upvotes: 0

Views: 159

Answers (1)

M'sieur Toph'
M'sieur Toph'

Reputation: 2676

Use e.stopPropagation(); to prevent propagation of the click event :

$('#removeBtn').click(function(e) {
    e.stopPropagation();
    $(this).parent('div').remove();
});

Upvotes: 1

Related Questions