Ah, but you still have non-standard markup. My second version is actually shorter:
Code:
jQuery(function($){
$('input.checkme').removeAttr('checked').click(function(){
$(this).next('div.clip')[this.checked? 'addClass' : 'removeClass']('clipped');
});
});
These bits:
Code:
function togAttr(index, attr){return !attr;}
and:
Code:
$('div.clip').click(function(){
$(this).prev('input.checkme').attr('checked', togAttr).click().attr('checked', togAttr);
});
are only required for increased functionality. With them clicking on the divisions also changes the checked state of the checkboxes and adds or removes the class clipped from the divisions.
Where you do:
Code:
$("input").removeAttr("checked");
$("input").click(function(){
if ($(this).is(":checked"))
{
$(this).next('div').addClass("clipped");
}
else
{
$(this).next('div').removeClass("clipped");
}
You are creating a jQuery for 'this' twice for each click, and a jQuery for 'input' twice. I have:
Code:
$('input.checkme').removeAttr('checked').click(function(){
$(this).next('div.clip')[this.checked? 'addClass' : 'removeClass']('clipped');
});
Only one for each. This may seem like a small difference, but in longer code it will add up, increasing execution time. The reason I use 'input.checkme' and 'div.clip' instead of just 'input' and 'div' is that your markup likely will grow to include other inputs possibly followed by other divs that shouldn't receive this treatment.
Bookmarks