0

I wrote code inside a form tag, when the user clicks on the radio button, courses should appear and these courses are related to their major, it was working fine, but when I put the form tag it doesn't work anymore. I used the form tag because of PHP.

JavaScript:

<script>
  function cs() {
    if (document.getElementById("cs").checked == true) {
      document.getElementById("isco").style.display = "none";
      document.getElementById("cnco").style.display = "none";
    }
    document.getElementById("csco").style.display = "initial";
  }

  function is() {
    if (document.getElementById("is").checked == true) {
      document.getElementById("csco").style.display = "none";
      document.getElementById("cnco").style.display = "none";
    }
    document.getElementById("isco").style.display = "initial";
  }

  function cn() {
    if (document.getElementById("cn").checked == true) {
      document.getElementById("isco").style.display = "none";
      document.getElementById("csco").style.display = "none";
    }
    document.getElementById("cnco").style.display = "initial";
  }
</script>

div style="background-color: white; text-align: left; padding-left:100px; padding-bottom: 10px">
<br><br><br>
<h3>Sign up</h3>

<form id="sign" name="sign" method="POST" action="sign.php">

  <label for="major">Major:</label>
  <input type="radio" name="major" id="cs" value="cs" onclick="cs()">CS
  <input type="radio" name="major" id="is" value="is" onclick="is()">IS
  <input type="radio" name="major" id="cn" value="cn" onclick="cn()">CN

  <div style="display:  none;" id="csco">
    <label for="courses">Select the courses you finished or takes currently: </label>
    <br><br>
    <input type="checkbox" name="courses" value="pr">Professional Responsibility
    <input type="checkbox" name="courses" value="se">Software Engineering
    <input type="checkbox" name="courses" value="alg">Analysis and Design of Algorithms
    <input type="checkbox" name="courses" value="web">Web-based Systems
    <br>
  </div>
  <div style="display: none;" id="isco">
    <label for="courses">Select the courses you finished or takes currently: </label>
    <br><br>
    <input type="checkbox" name="courses" value="web">Web-based Systems
    <input type="checkbox" name="courses" value="sad">System Analysis and Design(2)
    <br>
  </div>
  <div style="display: none;" id="cnco">
    <label for="courses">Select the courses you finished or takes currently: </label>
    <br><br>
    <input type="checkbox" name="courses" value="np">Introduction to Network Programming.
    <input type="checkbox" name="courses" value="nd">Network Design or Network Simulation and Modeling.
    <br>
  </div>
  <br>

  <button name="sig" id="sig" style="padding: 10px">Sign up</button>

</form>
</div>

is there any way to make the JavaScript work?

1 Answer 1

1

Yes, avoid adding listeners direct in the html. Always try to add them by script.

The below code works. Look that I added the onclick listeners inside the JS script, using
document.getElementById("elementId").onclick = functionToAssign
(function name without parentheses in this case).

Don't forget to remove the onclick from HTML.

Your current code is not working probably because that when HTML is rendered, the functions are not yet defined.

function cs() {
  if (document.getElementById("cs").checked == true) {
    document.getElementById("isco").style.display = "none";
    document.getElementById("cnco").style.display = "none";
  }
  document.getElementById("csco").style.display = "initial";
}
document.getElementById("cs").onclick = cs

function is() {
  if (document.getElementById("is").checked == true) {
    document.getElementById("csco").style.display = "none";
    document.getElementById("cnco").style.display = "none";
  }
  document.getElementById("isco").style.display = "initial";
}
document.getElementById("is").onclick = is

function cn() {
  if (document.getElementById("cn").checked == true) {
    document.getElementById("isco").style.display = "none";
    document.getElementById("csco").style.display = "none";
  }
  document.getElementById("cnco").style.display = "initial";
}
document.getElementById("cn").onclick = cn
<div style="background-color: white; text-align: left; padding-left:100px; padding-bottom: 10px">
<br><br><br>
<h3>Sign up</h3>

<form id="sign" name="sign" method="POST" action="sign.php">

  <label for="major">Major:</label>
  <input type="radio" name="major" id="cs" value="cs">CS
  <input type="radio" name="major" id="is" value="is">IS
  <input type="radio" name="major" id="cn" value="cn">CN


  <div style="display:  none;" id="csco">
    <label for="courses">Select the courses you finished or takes currently: </label>
    <br><br>
    <input type="checkbox" name="courses" value="pr">Professional Responsibility
    <input type="checkbox" name="courses" value="se">Software Engineering
    <input type="checkbox" name="courses" value="alg">Analysis and Design of Algorithms
    <input type="checkbox" name="courses" value="web">Web-based Systems
    <br>
  </div>
  <div style="display: none;" id="isco">
    <label for="courses">Select the courses you finished or takes currently: </label>
    <br><br>
    <input type="checkbox" name="courses" value="web">Web-based Systems
    <input type="checkbox" name="courses" value="sad">System Analysis and Design(2)
    <br>
  </div>
  <div style="display: none;" id="cnco">
    <label for="courses">Select the courses you finished or takes currently: </label>
    <br><br>
    <input type="checkbox" name="courses" value="np">Introduction to Network Programming.
    <input type="checkbox" name="courses" value="nd">Network Design or Network Simulation and Modeling.
    <br>
  </div>
  <br>

  <button name="sig" id="sig" style="padding: 10px">Sign up</button>

</form>
</div>

Sign up to request clarification or add additional context in comments.

2 Comments

where should i put the script tag? i try the code in my editor put it does not work, but when i run it here , it work!
You really need the script inside the html as a tag? Can't you use a external JS file? If you really want to use the script inside HTML, add it as you are adding before, just substitute your current code by the one I have above, it must work. (try it using the <script> after the HTML)

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.