0

I am using a onclick function on my form submit button to hide a div. but it is not hiding. what is the best way to do this? I want to hide a div on my page, after submitting a form.

<form action="" method="POST"> <input type="submit" onclick="hide_group_posts();"> </form> <div id='div_i_want_to_hide'> <?php include $_SERVER['DOCUMENT_ROOT']."page.php";?> </div> <script> function hide_group_posts(){ $('#div_i_want_to_hide').hide(); } </script> 
6
  • Are you using ajax to submit the form? Commented May 6, 2012 at 0:45
  • Maybe it's because the hide_group_posts was declared after the button? Anyway don't use inline code if you're using jQuery. Commented May 6, 2012 at 0:47
  • i am not using ajax to submit the form, must i use ajax? Commented May 6, 2012 at 0:50
  • If you're not using ajax then you'll have to detect that the form was submitted server side and then hide #div_i_want_to_hide. Commented May 6, 2012 at 0:53
  • @gdoron i typed alert($) and i got a message that was not undefined. it said the jquery object is actually just the init constructor. so i dont think the issue is with not having a jquery reference Commented May 6, 2012 at 0:56

1 Answer 1

9
$('form').submit(function(){ $('#div_i_want_to_hide').hide(); }); 

If it didn't help you, one of the following must be true:

  • You didn't reference the jQuery library.
  • You didn't wrap the code with the DOM ready event.
  • You got typos.
  • The submit function worked, and you got new page, because you didn't prevent the default.

Prevent it like this:

$('form').submit(function(e){ $('#div_i_want_to_hide').hide(); e.preventDefault(); // Or with: return false; }); 
Sign up to request clarification or add additional context in comments.

3 Comments

i tried this and it is not working. by the way, i am not using ajax to submit the form, so must i use ajax for this to work?
Yes use ajax to submit form otherwise i don't think there is any reason of hiding div when you submit form.
@arboles. Cool I was glad to help. Don't forget to accept an answer(this one, or write one yourself if mine didn't help).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.