Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a JQuery/JavaScript code that I am using in a script. For some reason which I can't seem to find, the Hide() function is not working. What could be the issue? What did I do wrong?

JavaScript
$("#vertebrateAnimalsYes").hide();

$("#vertebrateAnimals").setOnchange(function(newVal, oldVal) {
    if (newVal == "no")
        $("#vertebrateAnimalsYes").disableValidation();
  		$("#vertebrateAnimalsYes").hide();
    if (newVal == "yes")
        $("#vertebrateAnimalsYes").enableValidation();
  		$("#vertebrateAnimalsYes").show();
});


What I have tried:

JavaScript
$("#vertebrateAnimalsYes").hide();

$("#vertebrateAnimals").setOnchange(function(newVal, oldVal) {
    if (newVal == "no")
        $("#vertebrateAnimalsYes").disableValidation();
  		$("#vertebrateAnimalsYes").hide();
    if (newVal == "yes")
        $("#vertebrateAnimalsYes").enableValidation();
  		$("#vertebrateAnimalsYes").show();
});
Posted
Updated 2-Mar-22 5:46am
Comments
Richard Deeming 2-Mar-22 9:49am    
Check your browser's developer tools for errors. Make sure your document contains precisely one element with the specified ID. Make sure your code isn't running before the DOMContentLoaded event.

Beyond that, nobody can help you. Debugging your code requires access to your page, so only you can debug it.
Computer Wiz99 2-Mar-22 10:05am    
Thanks. I found it and updated my solution.

JavaScript isn't Python, indentation does not create code blocks.

As you wrote it originally, disableValidation and enableValidation are executed conditionally, whereas Hide() and Show() are always executed (in that order), so nothing gets hidden.

Your "solution 1" switched things around, I doubt it is what you really want. Your code needs brackets so it executes either the first two or the last two method calls, and not three out of four of them.
 
Share this answer
 
v2
JavaScript
$("#vertebrateAnimals").setOnchange(function(newVal, oldVal) {
    if (newVal == "no")
        $("#vertebrateAnimalsYes").hide();
  		$("#vertebrateAnimalsYes").disableValidation();
    if (newVal == "yes")
        $("#vertebrateAnimalsYes").show();
  		$("#vertebrateAnimalsYes").enableValidation();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900