~ / archivo / stackoverflow / 44289338
jQuery .hide() method not hiding div
-
I tried looking around on this site but none of the previous answers have worked in helping with this. I have a div that holds a header and form.
Basically I want once the form is submitted to hide everything that's in the div, so I've been trying to hide the div. But unfortunately nothing I do is getting this work. Here is my code:
<html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> <script> $(document).ready(function(){ $("#shirtForm").submit(function(e){ $("#question").hide(); }); }); </script> </head> <body> <div id="question"> <h3>What colour shirt was Eamon wearing today?</h3> <form id="shirtForm" onsubmit="shirtValidator()"> <input type="radio" name="shirtColour" id="redShirt"> Red <br> <input type="radio" name="shirtColour" id="blueShirt"> Blue <br> <input type="radio" name="shirtColour" id="yellowShirt"> Yellow <br> <input type="radio" name="shirtColour" id="noShirt"> He isn't wearing a shirt <br> <input type="submit" value="Enter" onclick="shirtValidator()"> </form> </div> </body> </html>Thanks in advance for any help at all.
- Sreetam Das 2017-05-31 15:52 the code snippet seems to be working just fine
- Lixus 2017-05-31 15:53 ^ ye, it woks fine, besides not finding your
shirtValidator()method - Toxide82 2017-05-31 15:53 have you tried using a toggle instead of show and hide
- Jonas Wilms 2017-05-31 15:53 you may return false in the submit function
- Jonas Wilms 2017-05-31 15:54 @Toxide82 and this should work because... ?
- Steve 2017-05-31 15:56 Where is that
shirtValidatormethod? - Conor606 2017-05-31 15:57 Thanks for the quick replies guys. The shirtValidator method was one from earlier when I was trying the same thing but using just javascript instead of jQuery. I've removed that now. The only thing I don't understand, is it works here fine, however locally on my machine, it just seems to refresh. Nothing disappears
- j08691 2017-05-31 15:59 You're doing nothing to prevent the form from being submitted and the page reloading. I'm also failing at seeing why you call your function on a button click and the form submission. Belt and suspenders?
- Sandman 2017-05-31 16:01 Possible duplicate of jQuery hide form on submit?
-
The problem isn't your code, it is working, but things are happening too fast for you to notice them.
The hide() function is working, the thing is that probably your DOM is reloading after submitting the form and hide() happens so fast that the effect is only visible for an instant.
Try with this:
<script> $(document).ready(function(){ $("#shirtForm").submit(function(e){ e.preventDefault() $("#question").hide(); return false; }); }); </script>It worked for me.
I've found a cool example of using hide here.
-
In your code just add e.preventDefault():
$(document).ready(function(){ $("#shirtForm").submit(function(e){ e.preventDefault(); $("#question").hide(); }); });The e.preventDefault() method stops the default action of an element from happening.
For example:
- Prevent a submit button from submitting a form
- Prevent a link from following the URL
-
$(document).ready(function(){ $("#shirtForm").submit(function(e){ e.preventDefault() $("#question").hide(); }); });<html> <head> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> </head> <body> <div id="question"> <h3>What colour shirt was Eamon wearing today?</h3> <form id="shirtForm"> <input type="radio" name="shirtColour" id="redShirt"> Red <br> <input type="radio" name="shirtColour" id="blueShirt"> Blue <br> <input type="radio" name="shirtColour" id="yellowShirt"> Yellow <br> <input type="radio" name="shirtColour" id="noShirt"> He isn't wearing a shirt <br> <input type="submit" value="Enter"> </form> </div> </body> </html>