Question

I have this piece of html code:

<form id="cambia_stato" method="get" action="">
<input type="text" name="stato" id="frase_stato" value=""
onclick="this.value='';" onblur="setMessaggio()" maxlength="255"
/>
</form>

and I want to check the value of the "stato" field on submit. I tried two ways: using onsubmit is not triggered by pressing the Enter key:

<!-- nothing happens with the following -->
<form id="cambia_stato" method="get" action="" onsubmit="myFunc()"> 

So I tried checking the input each time a key is pressed, changing the input attributes adding a onkeypressed event:

<!-- nothing happens with the following -->
<input type="text" name="stato" id="frase_stato" value=""
onclick="this.value='';" onblur="setMessaggio()" maxlength="255"
onkeypressed="myFunc()" />

For now, the function myFunc() is defined as follow:

function myFunc ()
{
    alert('test');
}

Am I going wrong somewhere or do I need a submit button?

Was it helpful?

Solution

Add this to your javascript. This will capture the enter key.

    document.onkeydown = function () {
        if (event.keyCode === 13) {
            myFunc();
        }
    };
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top