简体   繁体   中英

Calling functions from js but it doesn't work

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?

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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM