How to detect if enter key is pressed in JS [duplicate] - javascript

This question already has answers here:
Prevent form submission on Enter key press
(20 answers)
Closed 3 years ago.
I need to execute a JavaScript function when the enter key is pressed, i tried KeyCode but I found out it's deprecated,I can't do it in JQuery because i'm executing it at the end of the code, so it would be too late because someone could press enter before JQuery execution. Thanks in advance

This is the simplest way:
document.onkeypress = function (e) {
if (e.keyCode == 13) alert("Enter");
};

Related

Calling an AJAX function in jQuery using the Enter Key [duplicate]

This question already has answers here:
How can I detect pressing Enter on the keyboard using jQuery?
(19 answers)
Closed 6 years ago.
I have a textbox in HTML:
<input id="myInfo">
I would like to run an AJAX function as soon as the user enters in some value into the textbox using jQuery. I want the function to be called as soon as the user presses the "Enter"
Can someone please provide a very simple illustration as to how this would be accomplished?
Thank you
You could do something like
document.getElementById('myInfo').addEventListener('keypress', function(event) {
// you could also do keyCode === 13
if (event.key === 'Enter') {
console.log('do ajax request here');
}
})
Demo here: http://codepen.io/3stacks/pen/jyVpQP?editors=1111
$('#myInfo').on('keydown', function(e){
if(e.keyCode == 13)
{
//your code
}
});

How to detect if an user clicked on refresh button or F5 or Ctrl+F5? [duplicate]

This question already has answers here:
How to check page is reloading or refreshing using jquery or javascript?
(4 answers)
Closed 6 years ago.
Edit: Is it a good thing to use?
window.onbeforeunload = function(e) {
};
In this function I should detect if user click refresh and if YES then do this: $_POST['enter'] = NULL;
If you are looking for capturing the key event.
This code can help you.
$(document.body).on("keydown", this,
function (event) {
if (event.keyCode == 116) { alert('F5 pressed!');
}
});

Enter key reloads Chrome Browser [duplicate]

This question already has answers here:
'Enter key' wont submit form in Firefox, but will in Chrome, why?
(2 answers)
Closed 9 years ago.
I have a inputfield and an eventlistener keypress, in Firefox erverything is fine. If I run it in Chrome and hit the enter key the browser refreshes and the page is loaded again.
$('#iputField').keypress(function(e) {
if (e.which == 13) {
var message = $('#iputField').val();
}
});
Probably the form is submiting, you have to stop the default event propagation to avoid that.
$('#iputField').keypress(function(e) {
if(e.which == 13) {
e.preventDefault();
var message = $('#iputField').val();
}
});
Try saying e.preventDefault(); after the function ends.
If you're using a form, you probably need to call e.preventDefault().
Handling the "Enter" / "Return" key in Chrome
Try preventing the default behaviour:
$('#iputField').keypress(function(e) {
if(e.which == 13) {
e.preventDefault().
var message = $('#iputField').val();
}
});

Submitting form in Browser and multithreading in javascript [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Prevent Users from submitting form by hitting enter
I wrote some simple code (you can also test it here )
<form >
<input id="id2" type='text' />
</form>
$("#id2").keydown(function(e) {
if (e.keyCode == 13) {
alert('nope!');
return false;
}
});
If I click enter I should see an alert with "nope!" and then nothing. But, after I click "ok" on the alert window, the form is submitting despite the function returning false.
What is it? Something strange multithreading in javascript? Bug? Or...
​
Use the keypress event rather than the keydown event:
$("#id2").keypress(function(e) {
if (e.keyCode == 13) {
alert('nope!');
return false;
}
});
Updated demo

How do I use the Enter key as an event handler (javascript)? [duplicate]

This question already has answers here:
How can I execute a function on pressing the enter key in an <input> field?
(12 answers)
Closed 4 years ago.
im trying to make my own chat... so i have an input text field, the submit button, isn't even submit, its just a button.... so when the enter key is pressed, i need the value of the input field to appear in my textarea (which is readonly)...
well look.. make long story short, i just want a basic enter key event handler, i know it works perfectly with submit buttons cus you don't need to program anything at all, its default. but my button is type="button" .... so when you press enter nothing happens... how do i trigger my button by pressing enter?
You could make the button type submit, or you can use the onkeyup event handler and check for keycode 13.
Here's a list of key codes: Javascript Char codes/Key codes). You'll have to know how to get the keycode from the event.
edit: an example
HTML:
<input onkeyup="inputKeyUp(event)" ...>
Plain javascript:
function inputKeyUp(e) {
e.which = e.which || e.keyCode;
if(e.which == 13) {
// submit
}
}
Here is a working code snippet for listening for the enter key
$(document).ready(function(){
$(document).bind('keypress',pressed);
});
function pressed(e)
{
if(e.keyCode === 13)
{
alert('enter pressed');
//put button.click() here
}
}
Here is a version of the currently accepted answer (from #entonio) with key instead of keyCode:
HTML:
<input onkeyup="inputKeyUp(event)" ...>
Plain javascript:
function inputKeyUp(e) {
if (e.key === 'Enter') {
// submit
}
}

Categories