1

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");
  }
}
1
  • 1
    Right, you set document.getElementById('course').onchange to be one thing then you set it to something else, overwriting the first. You could use element.addEventListener to add multiple event listeners, or you could combine the code in the two onchange functions into one function. Commented Aug 9, 2017 at 18:47

1 Answer 1

1

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");
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

It doesn't work they all disabled when I pick BS and TOP
If you pick TOP, only first to fourth will be disabled, and there is no BS, however, if you meant something from the first list then only the grades 11 and 12 will be disabled.
I mean if I pick any of BS, BSIT BSHRM etc.. , and when I pick gas etc.. they all will disable imgur.com/a/MNbhf
glad to help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.