views:

51

answers:

1

I have two JavaScript code snippets. These perform specific task when an 'Update' button is clicked.

I would like to merge them. Any help is appreciated.

JavaScript 1: When the button is clicked, it checks if at least one checkbox is selected:

function doUpdate(){
    var c = document.getElementsByTagName('input'); 
    for (var i = 0; i < c.length; i++) { 
        if (c[i].type == 'checkbox' && c[i].checked == true) { 
            // At least one checkbox is checked     
            document.holiDay.command.value= 'update';
            document.holiDay.submit();  
            return true; 
        } 
    } 
    // Nothing has been checked 
    alert("Please identify what warehouses comply:"); 
    return false; 
}

JavaScript 2: When any checkbox is checked and update button is clicked, check all of them or uncheck all of them if any checkbox is unchecked; then perform the update feature:

function doUpdate(){
    checked=false;
    function All (holiDay) {
        var all= document.getElementById('holiDay');
        if (checked == false){
            checked = true
        }
        else{
            checked = false
        }
        for (var i =0; i < all.elements.length; i++){ 
            all.elements[i].checked = checked;
        }
    }
    //after checked or unchecked all checkboxes then submit the form and other functionality
    document.holiDay.command.value= 'update';
    document.holiDay.submit();  
    return true; 
} 
+1  A: 

I'm really not sure what you want to do, but here's a stab at it:

function doUpdate(){
    var c = document.getElementsByTagName('input');
    for (var i = 0; i < c.length; i++) {
        if (c[i].type == 'checkbox' && c[i].checked == true) {
            // At least one checkbox is checked    
            UpdateHoliday();
            return true;
        }
    }
    // Nothing has been checked
    alert("Please identify what warehouses comply:");
    return false;
}

function UpdateHoliday(){
    checked = false;
    function All (holiDay) {
        var all = document.getElementById('holiDay');
        checked = !checked;
        for (var i =0; i < all.elements.length; i++){
            all.elements[i].checked = checked;
        }
    }
    //after checked or unchecked all checkboxes then submit the form and other functionality
    document.holiDay.command.value = 'update';
    document.holiDay.submit();  
} 

It would really help to simplify and indent your code so that we could understand it more clearly.

palswim