I have a checkall script that select all checkboxes via a checkbox click.
Currently it works great but I need multiple instances of it in the same form. So I need to modify it to only target the closest fieldset.
Here is the working code, also here is the fiddle, http://jsfiddle.net/clintongreen/22w3B/
$('.checkall').click(function () {
var checked = $(this).data('checked');
$('fieldset').find(':checkbox').attr('checked', !checked);
$(this).data('checked', !checked);
});
And here is my failed attempt:
$('.checkall').click(function () {
var checked = $(this).data('checked');
var next_set = $('fieldset').next();
$(next_set).find(':checkbox').attr('checked', !checked);
$(this).data('checked', !checked);
});
Just a one line change from your original attempt.
But why not just give the fieldsets their own IDs to make it a lot less fragile?