0

I am using a Bootstrap Pop up. This Pop up gets opened from Main Form.

In this Pop up, I have TextBox & a Button.

<input class="form-control" type="text">
<input type="button" value="Search" onclick="searchEmployee()" />

When I hit ENTER key when Pop up is open, the Main Form gets submitted. The Main Form has a submit button.

How can I avoid form submit in pop up on ENTER key press?

And on the contrary enable ENTER key submit, when pop up is closed.

Rory McCrossan
  • 331,213
  • 40
  • 305
  • 339
Anup
  • 9,396
  • 16
  • 74
  • 138
  • I need to prevent users from submitting form by hitting enter only when pop up is opened. – Anup Jan 12 '15 at 13:13
  • Then you can bind the little code to prevent the submit just for the form inside a modal, with a selector such as ```$('.modal form')``` – Gregoire D. Jan 12 '15 at 13:28

2 Answers2

4
$('#formid').on("keyup keypress", function(e) {
  var code = e.keyCode || e.which; 
  if (code  == 13) {               
    e.preventDefault();
    return false;
  }
});
Dhaval
  • 2,341
  • 1
  • 13
  • 16
0

Disabling the default behavior of a form submission can be achieved by using event.preventDefault(); by catching the event of the ENTER button being clicked.

There are other ways to simply disable all submits depending on how specific you want to be, best way is to identify a specific id:

document.getElementById("submitButtonId").disabled = true;

But you can also specify which submit buttons are to be disabled if they're identifed with a class or if you simply wish to disable all submits of all kinds by using getElementByClass or getElementsByTagName.

Community
  • 1
  • 1
Jonast92
  • 4,964
  • 1
  • 18
  • 32
  • This pop up is used in many places, so `id` is different every time. – Anup Jan 12 '15 at 13:11
  • @Anup You can use `getElementByClass` if the submit buttons have a provided class. You can use `getElementsByTagName` if it's suppose to match all elements of some specific type, i.e. `submit`. – Jonast92 Jan 12 '15 at 13:13