16

Here is the code for my modal in twitter bootstrap:

<div class="modal-body">
<form class="form-horizontal" method="POST" name="loginForm" id="loginForm">
<div class="control-group">
<label class="control-label" for="inputEmail">Email</label>
<div class="controls">
  <div class="input-prepend">
    <span class="add-on"><i class="icon-envelope"></i></span>
    <input class="span2" id="inputIcon" type="text" name="email"/>
  </div>
</div>
</div>
<div class="control-group">
<label class="control-label" for="inputPassword">Password</label>
<div class="controls">
  <div class="input-prepend">
    <span class="add-on"><i class="icon-lock"></i></span>
    <input class="span2" id="password" type="password" name="password"/>
  </div>
</div>
</div>
<div class="control-group">
<div class="controls">
<label class="checkbox">
<input type="checkbox"> Remember me
</label>
</form>
<button type="submit" class="btn" id="signin_button">Sign in</button>
</div>
</div>

Whenever I press the Sign In button it reloads the page. How can I possibly disable refreshing the page upon clicking the button?

madth3
  • 7,275
  • 12
  • 50
  • 74
RJ Ramirez
  • 269
  • 1
  • 4
  • 16

3 Answers3

22

You could prevent the default behavior of the button that is submitting the page.

Here is a link to the Mozilla Developer Network on "preventDefault": event.preventDefault

In jQuery you can do this like this:

$("#signin_button").on("click", function(e) {
    e.preventDefault();

    // the rest of your code ...
});
MANJEET
  • 1,733
  • 2
  • 12
  • 21
Jo David
  • 1,696
  • 2
  • 18
  • 20
16

Try changing the input type="submit" to a

<button class="btn" id="signin_button">Sign in</button> 

or

<input type="button" class="btn" id="signin_button" value="Sign in"/>

if your objective is not to submit the form.

May also be useful: Difference between <input type='button' /> and <input type='submit' />

Community
  • 1
  • 1
cernunnos
  • 2,766
  • 1
  • 18
  • 18
2

You can use click also:

$("#signin_button").click(function (e) {
  e.preventDefault();
  // code
}