mongmongmong seesee February 2016

How to press enter in input type text call function javascript by not submit form?

How to press enter in input type text call function javascript by not submit form ?

When fill data into input type text and press enter keyboard i want only to call function detect_enter_keyboard(event) by not to submit form.

How can i do that ?

https://jsfiddle.net/atuyx7qc/

<form>
<input type="text" name="user" onkeypress="detect_enter_keyboard(event)">
<input name="btnLogin" onclick="test_fn();" type="button" value="Send">
</form>

<script>
function detect_enter_keyboard(event) {
    var key_board_keycode = event.which || event.keyCode;
    if(key_board_keycode == 13)
    {
        test_fn();
    }
}
</script>

<script>
function test_fn(){
alert("555555");
}
</script>

Answers


LostMyGlasses February 2016

With preventDefault(). Add this line after if(key_board_keycode == 13):

event.preventDefault();


Gomzy February 2016

use this

function detect_enter_keyboard(event) {

    var key_board_keycode = event.which || event.keyCode;
    if(key_board_keycode == 13)
    {
        event.preventDefault();
        test_fn();
    }
}
function test_fn(){
    alert("555555");
}
<form >
<input type="text" name="user" onkeypress="detect_enter_keyboard(event)">
<input name="btnLogin" onclick="test_fn();" type="button" value="Send">
</form>

Post Status

Asked in February 2016
Viewed 1,560 times
Voted 9
Answered 2 times

Search




Leave an answer