Simulate enter key in jquery or javascript [duplicate] - javascript

This question already has answers here:
How can I automatically trigger an enter key press event after a certain action is executed?
(4 answers)
Closed 7 years ago.
I am trying to let jquery press the enter for me when the page loads. I already tried a few things, but they did not work. I would rather not use any plugins. So what i want is that jquery puts the focus on my input field and presses enter. This is my current code.
$(document).ready(function() {
$('#test').focus().trigger({ type : 'keypress', which : 13 });
});

You can use jQuery.Event constructor
$('body').on('keypress', function(e) {
console.log(e.which);
});
// Create a new jQuery.Event object with specified event properties.
var e = jQuery.Event("keypress", {
which: 13
});
// trigger an artificial keypress event with which 13
jQuery("body").trigger(e);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

in your on load function/ready function
var e = jQuery.Event("keydown");
e.which = 13;
$('#test').focus();
$("#test").trigger(e);

Related

jquery 'keypress' event not detecting pressing the 'delete' key [duplicate]

This question already has answers here:
Capturing "Delete" Keypress with jQuery
(5 answers)
Closed 2 years ago.
For some reason when I try to detect if the delete key is pressed. It doesn't work but it does detect other keys being pressed does anybody know why this is happening?
code:
$('p:last-child').bind(
'keypress',
(data = (e) => {
console.log(e.keyCode)
if (e.keyCode == 8) {
console.log("delete key presssed")
}
}))
The problem here is that you are attaching an event to p:last-child, try to attach it to input.
Or if you just want to capture delete keypress you need to attach event to the document, like this:
$(document).on('keypress', e => console.log(e.keyCode));

Prevent enter from writing new line in content edititable div [duplicate]

This question already has answers here:
Prevent line/paragraph breaks in contentEditable
(11 answers)
Closed 2 years ago.
How can I prevent the user from writing a new line with the enter key in a contenteditable div?
So far I have tried (the id of my editable div is editable:
$('#editable').on('keyup', function(e) {
if (e.which == 13) {
event.preventDefault();
}
});
However this does not work.
Is this possible using JS and jQuery?
Edit
I have been informed that pressing enter does not insert a new line so I have put a picture showing when happens when I press enter inside the editable div.
I would like code to remain on one line basically, even if the user presses enter.
Instead of keyup, use keydown.
const ediatble = document.querySelector('#editable');
ediatble.addEventListener('keydown', e => {
if (e.key === 'Enter') {
e.preventDefault();
}
});
See example on JSFiddle

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();
}
});

Categories