Reputation: 1322
What is the best way to add opposite of Not of jquery?
<div class="line-item">
<div class="thumb">
Image goes here
</div>
<div class="title">
Title goes here
</div>
<div class="destroy">
Destroy Button goes here
</div>
here is the Java Script
$('div.line-item').delegate('div:not(".destroy")', 'click', function() {
alert('hi');
})
What i want is to apply "click" on just that div having .destroy class
here is the working Demo: http://jsfiddle.net/trVKF/110/
Upvotes: 0
Views: 82
Reputation: 10724
Try this for binding the click event without using delegate.
$('.line-item > .destroy').click(function(){
//your code goes here
});
Upvotes: 1
Reputation: 3504
$('div.line-item').on('click', '.destroy', function() {
alert('hi');
});
If you are using a newer version of jQuery.
Upvotes: 1
Reputation: 1317
would that achieve what you need ?
$('div.line-item').delegate('div:not(".destroy")', 'click', function() {
alert('hi');
}).delegate(".destroy", 'click', function(){
alert('bye');
})
http://jsfiddle.net/trVKF/112/
Alternatively:
$('div.line-item').delegate('div', 'click', function(e) {
if($(e.target).is(".destroy")){
alert("bye");
}else{
alert("hi");
}
});
http://jsfiddle.net/trVKF/113/
Upvotes: 1
Reputation: 318302
Use a newer version of jQuery and do :
$('div.line-item').on('click', '.destroy', function() {
// ^^ static parent | ^^ event | ^^ dynamic element bound to handler
alert('hi');
});
Upvotes: 2
Reputation: 66693
You can use:
$('div.line-item').delegate('.destroy', 'click', function() {
alert('hi');
})
as in this Demo
Upvotes: 1