Reputation: 21
I am trying to combine two functions together so they both simultaneously. How can I do this?
Here is the JavaScript:
function showTicks()
{
if (chooseShield.value == ("ely"))
{
document.getElementById("cb1").disabled=false;
document.getElementById("cb2").disabled=false;
document.getElementById("cb3").disabled=false;
document.getElementById("cb4").disabled=false;
document.getElementById("cb5").disabled=true;
document.getElementById("cb6").disabled=true;
document.getElementById("cb7").disabled=true;
document.getElementById("cb8").disabled=true;
document.getElementById("cb9").disabled=true;
}
}
function checkchecks()
{
if(likeShield.value == ("noShield") && chooseShield.value == ("ely") && (cb3.checked == true || cb4.checked == true))
{
document.getElementById("cb2").checked=true;
}
likeShield.onchange=checkchecks
chooseShield.onchange=checkchecks
cb3.onchange=checkchecks
cb4.onchange=checkchecks
}
function doBoth() {
checkAll();
checkchecks();
}
likeShield.onchange=doBoth;
chooseShie;d.onchange=doBoth;
cb3.onchange=doBoth;
cb4.onchange=doBoth;
The first function disables all the checkboxes except 1, 2, 3 and 4. The second function should then allow a user to click 3 or 4 which would automatically check checkbox 2. But only the second function works and the first one is ignored. The onchange = "showTicks()"
in the html.
Upvotes: 0
Views: 147
Reputation: 382150
You can do this :
var doBoth = function(){
showTicks();
checkchecks();
};
likeShield.onchange=doBoth;
chooseShield.onchange=doBoth;
cb3.onchange=doBoth;
cb4.onchange=doBoth;
Another option would be to use addEventListener :
likeShield.addEventListener('change', showTicks);
likeShield.addEventListener('change', checkchecks);
but it would be more verbose in your case.
Of course, if your two functions serve no other purpose, the simplest would be to take the code of the showTicks function and put it into the checkchecks function.
Also, don't forget to ensure variables like chooseShield
are defined. Here's how you can define your variables :
var chooseShield = document.getElementById('chooseShield');
var likeShield = document.getElementById('likeShield');
for (var i=1; i<10; i++) window['cb'+i]=document.getElementById('cb'+i);
Upvotes: 2