Showing posts with label javascript. Show all posts
Showing posts with label javascript. Show all posts

Saturday, March 15, 2008

javascript: How to select/deselect checkboxes

This function will select/deselect, tick/untick, check/uncheck all checkboxes belonging to a group. It can be used by a button to toggle a checkbox collection (e.g. Struts multibox in DisplayTag).


function toggle (checkboxes, isSelectAll)
{
if (checkboxes != null)
{
for (i = 0; i < checkboxes.length; i++)
{
checkboxes[i].checked = isSelectAll;
}
}
}


Usage: You can call this function on two buttons:
- select button: onclick="CheckboxUtil.toggle(document.formName.checkboxName, true)"
- deselect button: onclick="CheckboxUtil.toggle(document.formName.checkboxName, false)"


where:
- formName = name of the form.
- checkboxName = name of the checkbox group

Make sure all your checkboxes have the same checkboxName.

Sunday, January 22, 2006

javascript: Sorting Date in an Array (Collection)



function getDateLatest()
{
var date0 = new Date('12/31/2005');
var date1 = new Date('11/29/2007');
var date2 = new Date('01/13/1999');

var dateArray = new Array();

//we store date as time in milliseconds.
//getTime() returns the number of milliseconds since
//midnight of January 1, 1970. This method is also very
//useful for generating a unique sequence of numbers.
dateArray[0] = date0.getTime();
dateArray[1] = date1.getTime();
dateArray[2] = date2.getTime();
dateArray.sort(sortNumber);

var dateLatest = new Date(dateArray[dateArray.length - 1]);
alert(dateLatest);}

//By default, Array sorts alphabetically. To get it to sort
//numerically, we write a function that compares numbers.
function sortNumber(a, b)
{
return a - b
}