Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,542
    Thanks
    45
    Thanked 259 Times in 256 Posts

    Prevent input from submitting form

    I was curious if there is a method to keep a single input from submitting a form.

    I have an input into which I'm building a drop down search feature, however, I'd like it so when the user presses return there, instead of submitting the form, it runs an AJAX POST instead. I am currently using keyUp to recognize the button press, but its still submitting the form its attached to. Obviously, I'd like that not to happen.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,733
    Thanks
    80
    Thanked 4,515 Times in 4,479 Posts
    Code:
    <form ... onsubmit="return false;">
    If, under some circumstances, you DO want to submit the form, just use
    Code:
    <input type="button" value="Submit" onclick="this.form.submit()" />
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,542
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Ah, so a case of "stop all, and then allow what you want". No way to stop the individual elements alone I guess.

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    26,733
    Thanks
    80
    Thanked 4,515 Times in 4,479 Posts
    You probably could, but using onKeyDown instead of onKeyUp. By the time you capture onKeyUp, the damage is already done.

    But mine is a kind of "catch all" solution.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,542
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Aye, and it should work fine. I'm using jQuery, so selecting all the inputs sans the ones I don't want forcing will be easy to do. And the site, as built, needs javascript to work and informs the user of such, so its not even like the solution without javascript is an issue.

  • #6
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,642
    Thanks
    0
    Thanked 649 Times in 639 Posts
    Quote Originally Posted by Keleth View Post
    the site, as built, needs javascript to work and informs the user of such, so its not even like the solution without javascript is an issue.
    So your site is broken for people without JavaScript - most of whom have JavaScript disabled for a reason such as where enabling it would make it impossible for them to use their browser at all due to not being told what is happening when your script changes the page after they finished listening to it in their browser. That's a BIG issue.

    Any web page that doesn't work without JavaScript is a major design issue.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #7
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,542
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Quote Originally Posted by felgall View Post
    So your site is broken for people without JavaScript - most of whom have JavaScript disabled for a reason such as where enabling it would make it impossible for them to use their browser at all due to not being told what is happening when your script changes the page after they finished listening to it in their browser. That's a BIG issue.

    Any web page that doesn't work without JavaScript is a major design issue.
    So I'm guessing you think Google Analytics has a major design issue? Or Pandora? Or the plethora of other major sites out right now that require JS to run? You will rarely run into a major site these days that doesn't require JS for something, and when disabled as a whole, ends up making the site only partially functional (if you wanna talk a design issue, how about the sites that require JS simply to render properly). The site I'm building requires JS to simply do what it does... it has an interactive map, a chat system, etc. So far, for those pages, with JS it simply informs the user that in order to use this part of the site, you need to activate JS. And the more features I build that requires JS to operate, the more I realize I need JS on the site as a whole to truely make it work.

    Now mind you, the page in question here, needs JS for its core functionality. So its not a design issue, but a requirement.

    And I'd contend that most people with JS disabled have it disabled for security reasons, by their choice or not.

  • #8
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,542
    Thanks
    45
    Thanked 259 Times in 256 Posts
    As a note for the original issue: you can disable an individual input from submitting not via keyDown but rather with keypress. So with keypress, detect 13, and return false.


  •  

    Posting Permissions

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