TAGS :Viewed: 22 - Published at: a few seconds ago

[ How to avoid refreshing the page in a bootstrap modal? ]

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?

Answer 1


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 ...
});

Answer 2


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' />

Answer 3


You can use click also:

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