1

I've tried a few suggestions from this site on preventing SUBMIT when the user presses the ENTER key on a form, but they don't seem to work. I'm either not putting the SCRIPT in the correct place or maybe it's because I'm using a MODAL?

Here's one of the scripts I tried:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js">
            $(document).ready(function() {
              $(window).keydown(function(event){
                if(event.keyCode == 13) {
                 event.preventDefault();
                  return false;
                }
              });
             });
</script>

I've put the code after the FORM tags, nested it in the BUTTON SUBMIT tags. Where am I going wrong? The form is a simple form with inputs and selects with some PHP to populate the select tags.

4
  • 1
    I recommend declaring jquery as a separate script and then just using <script>//your code</script> Commented Mar 15, 2018 at 8:53
  • @ThisGuyHasTwoThumbs - I'm not sure what you mean by declaring it as a separate script? Commented Mar 15, 2018 at 9:03
  • so declare one script like <script src="/path/to/jquery.js"></script> then another like <script>$(document) //rest of code</script> - keeps the code clean and easier to manage :) Commented Mar 15, 2018 at 9:06
  • Winner Winner Chicken Dinner!! - Thanks @ThisGuyHasTwoThumbs - that worked... yes, I play PUBG :) Commented Mar 15, 2018 at 9:12

1 Answer 1

2

When there is src attribute in <script> any content inside tag is ignored. (docs)

You have to create two tags. One for jQuery CDN and one for your code:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function() {
    $(window).keydown(function(event){
        if(event.keyCode == 13) {
            event.preventDefault();
            return false;
        }
    });
});
</script>
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for explaining about the code inside the SRC tag being ignored...
I wanted to mark this but you gave me the correct answer first so I was a bit hesitant. I have marked it now...
@Rayza I only commented :) don't worry bout it ;) :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.