Avoid modal dismiss on enter keypress

FormsTwitter BootstrapModal Dialog

Forms Problem Overview


I have set up a bootstrap modal with a form inside it, I just noticed that when I press the Enter key, the modal gets dismissed. Is there a way not to dismiss it when pressing Enter?

I tried activating the modal with keyboard:false, but that only prevents dismissal with the ESC key.

Forms Solutions


Solution 1 - Forms

I just had this problem too.
My problem was that i had a close button in my modal

<button class="close" data-dismiss="modal">&times;</button>

Pressing enter in the input field caused this button to be fired. I changed it to an anchor instead and it works as expected now (enter submits the form and does not close the modal).

<a class="close" data-dismiss="modal">&times;</a>

Without seeing your source, I can't confirm that your cause is the same though.

Solution 2 - Forms

Just add the type="button" attribute to the button element, some browsers interpret the type as submit by default.

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#Attributes

This applies for all the buttons you have in the modal.

<button type="button" class="close" data-dismiss="modal">×</button>

Solution 3 - Forms

I had this problem even after removing ALL buttons from my Bootstrap Modal, so none of the solutions here helped me.

I found that a form with a single text field would cause the browser to do a form submit (and result in dismiss), if you hit Enter while keyboard focus is on the text field. This seems to be more of a browser/form issue than anything with Bootstrap.

My solution was to set the form's onsubmit attribute to onsubmit="return false"

This may be a problem if you are actually using the submit event, but I'm using JS frameworks that generate AJAX requests rather than doing a browser submit, so I prefer disabling submit entirely. (It also means I don't have to manually tweak every form element that might trigger a submit).

More info here: https://stackoverflow.com/questions/15239236/bootstrap-modal-dialogs-with-a-single-text-input-field-always-dismiss-on-enter-k

Solution 4 - Forms

You can put the login button before the cancel button and this would solve the issue you are having as well.

<div class="modal-footer">
    <button type="submit" class="btn primary">Login</button>
	<button type="submit" class="btn" data-dismiss="modal">Cancel</button>
</div>

Solution 5 - Forms

I had same problem, and i solved it with

<form onsubmit="return false;">

but there is one more solution, you can add dummy invisible input, so your form would look like this:

<form role="form" method="post" action="submitform.php">
    <input type="text" id="name" name="name" >
    <input type="text" style="display: none;">
</form>

Solution 6 - Forms

I had a similar experience just now and the way I solved it was instead of using a

Solution 7 - Forms

I had this problem too and I solved it this way. I added onsubmit to form. I also wanted to be able to use enter key as a saving key so I added save_stuff() javascript to onsubmit. return false; is used to prevent the form submit.

<form onsubmit="save_stuff(); return false;">
 ...
</form>

<script>
    function save_stuff(){
      //Saving stuff
    }
</script>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLuke MorganView Question on Stackoverflow
Solution 1 - FormsvishView Answer on Stackoverflow
Solution 2 - FormsjcalonsoView Answer on Stackoverflow
Solution 3 - FormsjpeskinView Answer on Stackoverflow
Solution 4 - FormsRyanView Answer on Stackoverflow
Solution 5 - FormsIgor LovrićView Answer on Stackoverflow
Solution 6 - FormswmockView Answer on Stackoverflow
Solution 7 - FormsFirzeView Answer on Stackoverflow