javascript – form with no action and where enter does not reload page-ThrowExceptions

Exception or error:

I am looking for the neatest way to create an HTML form which does not have a submit button. That itself is easy enough, but I also need to stop the form from reloading itself when submission-like things are done (for example, hitting Enter in a text field).

How to solve:

Add an onsubmit handler to the form (either via plain js or jquery $().submit(fn)), and return false unless your specific conditions are met.

Unless you don’t want the form to submit, ever – in which case, why not just leave out the ‘action’ attribute on the form element?

###

You’ll want to include action="javascript:void(0);" to your form to prevent page reloads and maintain HTML standard.

###

Simply add this event to your text field. It will prevent a submission on pressing Enter, and you’re free to add a submit button or call form.submit() as required:

onKeyPress="if (event.which == 13) return false;"

For example:

<input id="txt" type="text" onKeyPress="if (event.which == 13) return false;"></input>

###

When you press enter in a form the natural behaviour of form is to being submited, to stop this behaviour which is not natural, you have to prevent it from submiting( default behaviour), with jquery:

$("#yourFormId").on("submit",function(event){event.preventDefault()})

###

an idea:

<form method="POST" action="javascript:void(0);" onSubmit="CheckPassword()">
    <input id="pwset" type="text" size="20" name='pwuser'><br><br>
    <button type="button" onclick="CheckPassword()">Next</button>
</form>

and

<script type="text/javascript">
    $("#pwset").focus();
    function CheckPassword()
    {
        inputtxt = $("#pwset").val();
        //and now your code
        $("#div1").load("next.php #div2");
        return false;
    }
</script>

###

Two way to solve :

  1. form’s action value is “javascript:void(0);”.
  2. add keypress event listener for the form to prevent submitting.

###

The first response is the best solution:

Add an onsubmit handler to the form (either via plain js or jquery
$().submit(fn)), and return false unless your specific conditions are
met.

More specific with jquery:

$('#your-form-id').submit(function(){return false;});

Unless you don’t want the form to submit, ever – in which case, why
not just leave out the ‘action’ attribute on the form element?

Writing Chrome extensions is an example of where you might have a form for user input, but you don’t want it to submit. If you use action=”javascript:void(0);”, the code will probably work but you will end up with this problem where you get an error about running inline Javascript.

If you leave out the action completely, the form will reload which is also undesired in some cases when writing a Chrome extension. Or if you had a webpage with some sort of an embedded calculator, where the user would provide some input and click “Calculate” or something like that.

Leave a Reply

Your email address will not be published. Required fields are marked *