3

I have been trying to hide a form after the submit button is clicked. I have googled and followed some but they seem to be not working for me.

This is my code:

HTML:

<div id="wrapper">
<form name="regForm" id="regForm" action="indexSave.php" method="post">

<table width="100%" border="0">
   <tr>
    <td colspan="2"> <center> Registration Form </center> </td>
   </tr>
   <tr>
    <td width="23%">&nbsp; Name</td>
    <td width="33%">&nbsp; <input type="text" name="name"  /> </td>
   </tr>
   <tr>
    <td>&nbsp; Email Address</td>
    <td>&nbsp;  <input type="text" name="email"  /></td>
   </tr>
   <tr>
    <td>&nbsp; Contact Number</td>
    <td>&nbsp;  <input type="text" name="number"  /></td>
   </tr>
   <tr>
    <td>&nbsp; Message</td>
    <td width="100">&nbsp; <textarea name="msg"> </textarea> </td>
   </tr>
   <tr>
    <td>&nbsp;  <input onclick="myClick()" name="submit" id="submit" type="submit" value="Submit"  /> </td>
    <td>&nbsp;</td>
  </tr>
</table>
</form>
</div>

And my Javascript:

<script type="text/javascript">
    function myClick() 
    {
        var me = document.getElementById("wrapper").visibility="false";
    }
</script>

I also tried this (from this link )

var me = document.getElementById("regForm").style.display="none";

Please help me figure out what's wrong?

1
  • 1
    Either of the answers below will work. Just to clear one thing up though... the visibility property should be either 'visible' or 'hidden' rather than a boolean. Commented Apr 27, 2014 at 5:03

4 Answers 4

3

You simply need:

function myClick(){
     var form = document.getElementById("regForm");
     form.style.display = "none";
}

DEMO

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

2 Comments

Thank you. I think my php is messing up with the javascript. It doesn't want to hide the form. >.<
yap. its the php. Thank you for the help
1

If hiding is all you want, you can have it with this really tiny piece of code:

<form onsubmit="this.style.display='none'" name="regForm" id="regForm" action="indexSave.php" method="post">

It will also hide the form when [enter] is pressed.

Comments

1

This code hides the form, reorganizing the other elements:

<script type="text/javascript">
    var form = document.getElementById("regForm");    
    function myClick(){
        form.style.display = "none";
    }
</script>

This code only makes it invisible, so if the code above is messing the layout, use it:

<script type="text/javascript">
    var form = document.getElementById("regForm");    
    function myClick(){
        form.style.visibility = "hidden";
    }
</script>

Comments

0
document.getElementById("regForm").style.display="none";

is enough to hide regForm. Try to place your <script> in <head>.

1 Comment

its in the head. i think the problem now is that my php is messing with the javascript

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.