www.webdeveloper.com
Results 1 to 5 of 5

Thread: Prevent Submit on Enter

Hybrid View

  1. #1
    Join Date
    Apr 2005
    Posts
    92

    Prevent Submit on Enter

    So I am using the following code to prevent a form submission when a user presses enter and everything works great expect one thing.....

    I have two forms on the page and I only want to apply this function to one of them, where as this code applies it to all text inputs.

    How would I go about applying this function only to a text input with the id "video_comment" ?

    Code:
    function stopRKey(evt) {
      var evt = (evt) ? evt : ((event) ? event : null);
      var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
      if ((evt.keyCode == 13) && (node.type=="text"))  {return false;}
    }
    
    document.onkeypress = stopRKey;

  2. #2
    Join Date
    Apr 2005
    Posts
    92
    Well, I replaced node.type with node.id and that seemed to do the trick!

  3. #3
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,270
    A variant consists to attach this event on your form and not on the document...

  4. #4
    Join Date
    Apr 2005
    Posts
    92
    Quote Originally Posted by 007Julien View Post
    A variant consists to attach this event on your form and not on the document...
    I'm certain that it does. Why don't you share that with everyone instead of just stating its existence.....

  5. #5
    Join Date
    Mar 2012
    Posts
    202
    i'm not completely sure, it's been awhile, but i think if you add onsubmit="return false" to the form tag it should prevent it from being physically submitted.. you can also make it call a function like this:

    Code:
    <form onsubmit="doForm(this); return false;">

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles