2

I have written a simple code and want to hide one div and show another on the click of a button.But nothing is happening, nothing is being shown on the console also.

    <!DOCTYPE html>
      <html lang="en">

      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="width=device-width,initial-scale=1">
        <title>Demo</title>
        <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
        <link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">

       <script type="text/javascript">     
        $(document).ready(function() {
          $('#register-btn').on("click", function() {
          $('#reg').hide();
        });
      });  
    </script>
     </head>
      <body>
        <div class="container" id="reg">
          <div class="row">
            <h1 style="text-align:center;">Demo</h1>
          </div>

         <form class="form-horizontal">
          <div id="call" class="col-md-6 col-md-offset-4">
            <div class="form-group">
              <div class="col-md-10">
                <input id="name" class="form-control"  type="text"  placeholder="Enter a Name"/>
              </div>
            </div>    

            <div class="form-group">
              <div class="row">
                <div class="col-md-4 col-md-offset-2">
                  <button class="btn-style" id="register-btn">Register</button>
                </div>
              </div>
            </div>
          </div>
         </form>
        </div> 

    </body>
</html>

Can anyone help me on this?

1
  • 1
    It works fine for me, I think your problem is that the page is reloading when you click the button since it is in a form Commented Aug 7, 2015 at 23:53

2 Answers 2

4

The default type of a button element is submit. When you click the element the form is submitted and the page is refreshed. Either use the type="button" attribute for the button element:

<button type="button" class="btn-style" id="register-btn">Register</button>

or prevent the default action of the event using preventDefault method of the event object.

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

Comments

0

you can return false; to disable the default behavior and it will work

$(document).ready(function() {
$('#register-btn').on("click", function() {

$('#reg').hide();
return false;
});
});  

Comments

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.