Combine two javascript scripts

I have two pieces of JavaScript code. They perform a specific task when you click the Refresh button.

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

JavaScript 1: When a button is clicked, it checks to see 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 a check box is selected and the refresh button is clicked, check all or clear the check box if all the boxes are unchecked; then execute the update function:

 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; } 
0
source share
1 answer

I'm really not sure what you want to do, but here is a hit on 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(); } 

This will really help simplify and defer your code so that we can understand it more clearly.

+2
source

Source: https://habr.com/ru/post/1315216/


All Articles