Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 874
RJ Ramirez Avatar asked Feb 18 '13 14:02

RJ Ramirez


People also ask

How do you prevent Bootstrap modal from closing when clicking outside?

When the Button is clicked, the HTML DIV is referenced using jQuery and its modal function is called along with properties data-backdrop: "static" and data-keyboard: false which disables the closing of the Bootstrap Modal Popup when clicked outside.

How do I stop Bootstrap modal pop up?

Clicking on the modal “backdrop” will automatically close the modal. Bootstrap only supports one modal window at a time.

How do you trigger a modal on page load?

Answer: Use the Bootstrap . modal('show') method modal('show') method for launching the modal window automatically when page load without clicking anything. A common example of this technique is loading the modal when user landed on the home page and requesting them to subscribe the website newsletter.


1 Answers

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 ...
});
like image 141
Jo David Avatar answered Sep 17 '22 20:09

Jo David