For example, we will use the Bootstrap style. And so we proceed.
We connect jQuery and Bootstrap:
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous"> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
Create a simple form:
<form class="my-form hide"> <div class="row"> <div class="col-md-6"> <div class="form-group"> <label for="name">Enter name:</label> <input id="name" type="text" placeholder="Enter name" class="form-control" /> </div> </div> <div class="col-md-6"> <div class="form-group"> <label for="email">Enter e-mail:</label> <input id="email" type="text" placeholder="Enter e-mail" class="form-control" /> </div> </div> </div> <div class="row"> <div class="col-md-12"> <div class="form-group"> <label for="text">Enter your message:</label> <textarea id="text" name="text" placeholder="Enter your message" class="form-control" rows="3"></textarea> </div> </div> </div> <div class="form-group text-right"> <input id="cancel" type="reset" value="Cancel" class="btn btn-default" /> <input id="enter" type="submit" value="Send" class="btn btn-success" /> </div> <p>Click "Cancel" to hide the form.</p> </form>
Add the link "Show form":
<a href="#" id="show-form">Show form</a>
We add the event of showing the form by clicking on the link:
$('#show-form').click(function () { $('.my-form').removeClass('hide'); $( "#name" ).focus(); });
Hide the form after clicking on the "Cancel" button:
$('#cancel').click(function () { $('.my-form').addClass('hide'); });
We wrap these events in the "document ready", for correct work:
<script> $(document).ready(function() { $('#show-form').click(function () { $('.my-form').removeClass('hide'); $( "#name" ).focus(); }); $('#cancel').click(function () { $('.my-form').addClass('hide'); }); }); </script>
Done, you can use. I will give the full page code:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Show/hide form - devreadwrite.com</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous"> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script> <script> $(document).ready(function() { $('#show-form').click(function () { $('.my-form').removeClass('hide'); $( "#name" ).focus(); }); $('#cancel').click(function () { $('.my-form').addClass('hide'); }); }); </script> </head> <body> <div class="container"> <div class="row"> <h1>Show/hide form</h1> <a href="#" id="show-form">Show form</a> <form class="my-form hide"> <div class="row"> <div class="col-md-6"> <div class="form-group"> <label for="name">Enter name:</label> <input id="name" type="text" placeholder="Enter name" class="form-control" /> </div> </div> <div class="col-md-6"> <div class="form-group"> <label for="email">Enter e-mail:</label> <input id="email" type="text" placeholder="Enter e-mail" class="form-control" /> </div> </div> </div> <div class="row"> <div class="col-md-12"> <div class="form-group"> <label for="text">Enter your message:</label> <textarea id="text" name="text" placeholder="Enter your message" class="form-control" rows="3"></textarea> </div> </div> </div> <div class="form-group text-right"> <input id="cancel" type="reset" value="Cancel" class="btn btn-default" /> <input id="enter" type="submit" value="Send" class="btn btn-success" /> </div> <p>Click "Cancel" to hide the form.</p> </form> </div> </div> </body> </html>