$('.dragbox').each(function(){
$('.close').click(function(){
$(this).parent().hide();
}),
$('.colpase').click(function(){
$(this).siblings('.dragbox_content').toggle();
})
});
views:
77answers:
5No. Presumably you want to apply the click handlers to matching elements within each dragbox. You can do that with:
$('.dragbox').each(function(){
$('.close', this).click(function(){
$(this).parent().hide();
}),
$('.colpase', this).click(function(){
$(this).siblings('.dragbox_content').toggle();
})
});
If you just wanted to add the handlers globally, you wouldn't want the each.
It doesn't look as if you need the each() function there. You may be applying the event handlers to the objects multiple times. Just:
$('.close').click(function(){
$(this).parent().hide();
});
$('.colpase').click(function(){
$(this).siblings('.dragbox_content').toggle();
});
Should do the trick.
Considering jquery works as a wrapped set (collection) i dont think you need the each method, just the
$('.dragbox').find('.close').click(function(){
$(this).parent().hide();
})
$('.dragbox').find('.colpase').click(function(){
$(this).siblings('.dragbox_content').toggle();
})
the handlers will be applied to all matched elements without the need for the each.
this will find all of the .close and .colpase inside of the .dragbox item(s) i assumed that is what you were after...
edited to use find in order to gain slight performance improvement. Thanks Dan/Alex.
function set_colors(pick_color) { var color_code=pick_color; $('#'+ color_owner).parent().css('background-color',color_code); }
if you're referring to "parent" to actual ". dragbox" in "each"
$('.dragbox').each(function(){
var self = this;
$(self).find('.close').bind("click", function(){
$(self).hide();
}); // <-- ","?
$(self).find('.colpase').bind("click", function(){
//This line confuses me because you could do the same in the selector for the click event
//unless you do have more things in the function
$(this).siblings('.dragbox_content').toggle();
});
/*
$(self).find('.colpase').siblings('.dragbox_content').bind("click", function(){
$(this).toggle();
});
*/
});