Reputation: 1630
I have layer A and layer B inside layer A
Is it possible to remove layer A with jQuery function remove()
clicking just on on layer A and not on layer B?
Upvotes: 4
Views: 133
Reputation: 2587
If you want .b1
to remain and delete just .a1
$('.a1').live('click',function() {
$(".b1", this).unwrap();
});
You can remove that div even if you don't know any child element:
$('.a1').live('click',function() {
$(".a1 :first-child").unwrap();
});
If you want to remove .a1
div if it's not clicked within .b2
this is what you need:
$('.a1').live('click',function() {
$(".a1 :first-child").unwrap();
});
$('.b1').live('click',function() {
throw "stop execution";
});
According to @Rick Calder's comment:
If you want you can use .addClass()
to change class or .removeClass()
to remove class.
Upvotes: 2
Reputation: 1610
Here's how I read the question:
If you click on .b1
, nothing should happen. If you click on .a1
it should be removed.
If that's the intent, here's the jQuery:
$('.b1').click( function(e){
e.stopImmediatePropagation();
});
$('.a1').click( function(){
$(this).remove();
});
Here's a jsbin. Note: css, html shamelessly stolen from @Vision's jsFiddle
Upvotes: 0
Reputation: 145458
$("body").on("click", ".a1", function(e) {
if (this === e.target) $(this).remove();
});
DEMO: http://jsfiddle.net/PhZhY/1/
Upvotes: 3