The requirement I am gonna illustrate now comes up in almost all development efforts where a grid or a tabular view is displayed. there is a single check box in the table header where upon click you need to change the checked status of all the check boxes in the grid
Here is a simple bit of code you can use to achieve that task
function(){
$('#' + allCheckBoxName + '').live("change", function (e) {
$("INPUT[type='checkbox']").attr('checked', $('#' + allCheckBoxName + '').is(':checked'));
});
}
Here "allCheckBoxName" is the master check box which is at the top of the grid.
But an issue arise when using this script with a drop down in the same page in IE browsers. that is in IE you need to bind check boxes events first before any other event bindings in order to work them properly. and strangely this is true with drop downs as well. so you face a strange dillema. "How can I bind events for two different kinds of controls at once?"
To overcome this just add the following code segment to the code
if ($.browser.msie) {
$('#' + allCheckBoxName + '').unbind("click");
$('#' + allCheckBoxName + '').live("click", function (e) {
$(this).trigger("change");
});
what it does is it rebinds the check box events after the drop down bind and trigger the change event.
so the completed function will look some thing like this
function () {
if ($.browser.msie) {
$('#' + allCheckBoxName + '').unbind("click");
$('#' + allCheckBoxName + '').live("click", function (e) {
$(this).trigger("change");
});
if (allCheckBoxVisibility) {
$('#' + allCheckBoxName + '').live("change", function (e) {
$("INPUT[type='checkbox']").attr('checked', $('#' + allCheckBoxName + '').is(':checked'));
});
}
}
$('#' + allCheckBoxName + '').live("click", function (e) {
$("INPUT[type='checkbox']").attr('checked', $('#' + allCheckBoxName + '').is(':checked'));
});
}
Hope it helps!!!
Welcome
"Happy are those who dream dreams and are ready to pay the price to make them come true"
About Me
My Home @ google Map
- Dhanushka Athukorala
- Colombo, Western Province, Sri Lanka
- I'm a Microsoft Certified Professional (MCP-ASP.net) who is currently working as a Software Engineer @ Aeturnum. I have experience in windows, web, mobile application development using .net framework 2.0, 3.5, 4.0 and .net compact framework 2.0 and 3.5 and i have also carried out development projects in C++, C, VB.net, Java, PHP, Pl/SQL, SQL, XNA, AJAX, Jquery etc...
Blog Archive
-
▼
2010
(16)
-
▼
November
(8)
- JavaScript setAttribute not working for IE
- Flot, A pure JQuery Charting tool and How to get t...
- Object Oriented JQuery/JavaScript
- Store a value in cookie from a class project in AS...
- Truncate a paragraph to the last word
- Remove duplicate values from a list
- Set the check status of all check boxes in a page ...
- Get all the selected check box IDs in page using J...
-
▼
November
(8)
Followers
Posted by
Dhanushka Athukorala
Monday, November 15, 2010
Subscribe to:
Post Comments (Atom)