A Day in the Life of a Developer
28 Mar
As I am nearing the end of my initial development phase of BackendPro there’s some useful bits of code I came up with to handle annoying but simple tasks. Over the next week or so I will be releasing a lot more small applications and libraries but felt this may help a few people. Its a small script to check and un-check multiple html check boxes. Its prime use is to save the user time from having to check/un-check many check boxes. It uses jQuery and is only 1/2 lines long.
$("input[name='all']").change(function(){
var parent = $(this);
$("input[name='"+parent.val()+"[]']",parent.parents('form:first')).each(function(){$(this).attr('checked',parent.is(':checked'));});
});
All you need to use it is the following HTML code.
<form> <input type="checkbox" name="all" value="myitems"> <input type="checkbox" name="myitems[]" value="1"> <input type="checkbox" name="myitems[]" value="2"> <input type="checkbox" name="myitems[]" value="3"> </form>
So when the user clicks on the check box with name all, all check boxes with the name specified in the value attribute of this ‘all‘ check box will be toggled. I hope someone finds it helpful.
3 Responses for "Multiple Check Checkbox"
12/10/09 cut & paste into homesite 5.5: result -> doesn’t work in any browser
.gainlayout { height: 1px; }
$(“input[name='all']“).change(function(){
var parent = $(this);
$(“input[name='"+parent.val()+"[]‘]”,parent.parents(‘form:first’)).each(function(){$(this).attr(‘checked’,parent.is(‘:checked’));});
});
All
1
2
3
hmmm?
I’m sorry I have no idea what that all meant?
I think he is saying that he tried it in several browsers (your code snippet and example) and it didn’t work.
Of course without seeing how he is using the snippet there is no way of knowing if there is something wrong with it, or is it just “operator error” ;p
Leave a reply