Ameri Ichinose
Ameri Ichinose

Reputation: 33

How to combine onchange in javascript

How to combine this javascript because I have problem when selecting the grade-11 and grade-12 the work did not work but only the BSCS pick by user thank you for help

document.getElementById('course').onchange = function() {


if (["BSCS", "BSIT","BSHRM","BSBM","BSTM"].indexOf(this.value) > -1) {
    document.getElementById("grade-11").setAttribute("disabled", true);
    document.getElementById("grade-12").setAttribute("disabled", true);


 } 
 else {
    document.getElementById("grade-11").removeAttribute("disabled");
    document.getElementById("grade-12").removeAttribute("disabled");
  }
}




document.getElementById('course').onchange = function() {


 if (["STEM", "TOP","GAS","HUMSS"].indexOf(this.value) > -1) {
    document.getElementById("first-year").setAttribute("disabled", true);


    document.getElementById("second-year").setAttribute("disabled", true);
    document.getElementById("third-year").setAttribute("disabled", true);
    document.getElementById("fourth-year").setAttribute("disabled", true);
  } else {



document.getElementById("first-year").removeAttribute("disabled");


    document.getElementById("second-year").removeAttribute("disabled");
    document.getElementById("third-year").removeAttribute("disabled");
    document.getElementById("fourth-year").removeAttribute("disabled");
  }
}

Upvotes: 1

Views: 172

Answers (1)

Djaouad
Djaouad

Reputation: 22766

This should work (based on your logic) :

document.getElementById('course').onchange = function() {

if (["BSCS", "BSIT","BSHRM","BSBM","BSTM"].indexOf(this.value) > -1) {
    document.getElementById("grade-11").setAttribute("disabled", true);
    document.getElementById("grade-12").setAttribute("disabled", true);
    document.getElementById("first-year").removeAttribute("disabled");
    document.getElementById("second-year").removeAttribute("disabled");
    document.getElementById("third-year").removeAttribute("disabled");
    document.getElementById("fourth-year").removeAttribute("disabled");

 } else if (["STEM", "TOP","GAS","HUMSS"].indexOf(this.value) > -1) {

    document.getElementById("first-year").setAttribute("disabled", true);
    document.getElementById("second-year").setAttribute("disabled", true);
    document.getElementById("third-year").setAttribute("disabled", true);
    document.getElementById("fourth-year").setAttribute("disabled", true);
    document.getElementById("grade-11").removeAttribute("disabled");
    document.getElementById("grade-12").removeAttribute("disabled");

  } else {

    document.getElementById("grade-11").removeAttribute("disabled");
    document.getElementById("grade-12").removeAttribute("disabled");
    document.getElementById("first-year").removeAttribute("disabled");
    document.getElementById("second-year").removeAttribute("disabled");
    document.getElementById("third-year").removeAttribute("disabled");
    document.getElementById("fourth-year").removeAttribute("disabled");
  }
}

Upvotes: 1

Related Questions