Call key press with javascript - javascript

I am trying to press tab and enter key through javascript/jquery on page load.
like:
if(event.keyCode == 13){
$("#submit").click();
}
this would only work when user presses the enter key but I want the javascript to do this stuff for the user on page load.

You can simulate a keypress with this code:
function simulateKeyPress(character) {
jQuery.event.trigger({ type : 'keypress', which : character.charCodeAt(0) });
}
thanks to this thread: Is it possible to simulate key press events programmatically?
but as Ghyath Serhal pointed out, there might be a better way than to simulate a keypress to then catch it by your same code.

Check the below if it helps
<body onload="$('#submit').click();"></body>

Related

How to detect ENTER keypress/ or the "GO" button in the Address Bar?

I want to detect the ENTER keypress of the Address Bar and also, the "Go(to the specified URL)" button using Javascript.
As per my previous efforts using "keycode==13" did not work as required.
say, in the following code:
window.onkeypress = testKeyEvent;
function testKeyEvent(e) {
if (e.keyCode == 13) //We are using Enter key press event for test purpose.
{
alert('Enter key pressed');
}
else //If any other button pressed.
{
alert('Not Enter key pressed');
}
} </script>
I want first the Alert box to be displayed,after I have typed any URL(valid or not) in the address box and Pressed ENTER/ Clicked GO button and then go to specified URL.
Is it Possible? I know I am missing out on a lot of things, Please mention about them.
If I am interpreting your question correctly, I don't think you can do this, because the context in which the JavaScript runs stops at the Document (meaning, JavaScript doesn't even quite know that the browser itself exists).
You can't detect the keystroke because it's outside your window, but you can detect navigation away from your page like
window.onbeforeunload = function () {
alert("Leaving page...");
}

How to detect pressing enter on mobile device keyboard using jquery?

I am using Bootstrap.When press enter in textbox ,open Modal popup with Bootstrap.But when I using mobile phone ,I cant detect pressing enter.How to detect it ?Can you help me please?
My code ;
<script type="text/javascript">
$(document).ready(function () {
$('.form-control').keyup(function (e) {
if (e.which == 13) {
$('.modal').modal('show');
}
});
});
</script>
Normally, within a form, the enter key on a mobile device submits the form. I suggest adding some logic in a submit handler:
$("#myForm").submit(function(){
// you're logic here
}
Additional information
See: HTML: Why does Android browser show "Go" instead of "Next" in keyboard?
Well, on touch devices there is no such event as keyup as far as I know. At least in iOS: https://developer.apple.com/library/archive/documentation/AppleApplications/Reference/SafariWebContent/HandlingEvents/HandlingEvents.html#//apple_ref/doc/uid/TP40006511-SW5
Consider using form's submit event instead.

Press the enter key in a text box with jQuery

How can I mimic pressing the enter button from within a <input>, using jQuery?
In other words, when a <input> (type text) is in focus and you press enter, a certain event is triggered. How can I trigger that event with jQuery?
There is no form being submitted, so .submit() won't work
EDIT
Okay, please listen carefully, because my question is being misinterpreted. I do NOT want to trigger events WHEN the enter button is pressed in textbox. I want to simulate the enter button being pressed inside the textbox, and trigger this from jQuery, from $(document).ready. So no method involving on.('keypress')... or stuff like that is what I'm looking for.
Use keypress then check the keycode
Try this
$('input').on('keypress', function(e) {
var code = e.keyCode || e.which;
if(code==13){
// Enter pressed... do anything here...
}
});
OR
e = jQuery.Event("keypress")
e.which = 13 //choose the one you want
$("#test").keypress(function(){
alert('keypress triggered')
}).trigger(e)
DEMO
Try this:
$('input').trigger(
jQuery.Event('keydown', { which: 13 })
);
try using .trigger() .Docs are here
Instead of using {which:13} try using {keyCode:13}.
$('input').trigger(jQuery.Event('keydown', {keyCode:13}));

how to make keyboard shortcut with key "c" from keyboard for calling a function

I am wondering if it is possible to call a jquery function from keyboard key.
Say i want to clear textbox values , if users press "c" key from her keyboard.
Now i am calling a function onclick event.Same function i want to call if users press "c" key from keyboard
$(".clear").click(function()
{
$('#from_amount').val('');
$('#to_amount').val('');
$('.clear').hide();
});
Please help me to solve this problem
Use the keypress event to handle this.
One possible problem with this will be, you won't be able to press c anywhere in the document, so you may want to support a key combination like ctrl + c. Or do not want to do this when the key is pressed within a input field
<script>
$(function(){
$(document).keypress(function(e){
if(e.which == 99){
$('#from_amount').val('');
$('#to_amount').val('');
$('.clear').hide();
}
})
})
</script>
Ignoring keypress from input fields
<script>
$(function(){
$(document).keypress(function(e){
if(e.which==99 && !$(e.target).is(':input')){
console.log('clear')
}
})
})
</script>

ENTER event for jQuery

I am new to javascript; thus, it question may seem to be naive. I have a simple jQuery function as
$(document).ready(function(){
$('#txtValue').keyup(function(){
sendValue($(this).val());
});
});
This sends immediately when a letter is typed in
I explored jQuery events, but I was unable to find an event for ENTER. I want to run the function when I typed all letters and pressed ENTER.
Check the keyCode property of the event object. 13 represents the Enter key:
$('#txtValue').keyup(function(e){
if(e.keyCode === 13) {
sendValue($(this).val());
}
});
The keyup event will fire every time a key is released. There is no way to selectively fire the event, but you can choose when to handle it!
Edit
It's actually better to use event.which, to deal with all browsers, as jQuery normalises the event object to help.
There isn't an event for an individual key, you'll need to bind the keypress event and see if the keyCode is the Enter key (13):
$('#txtValue').keypress(function(e) {
if (e.keyCode == 13) {
//do stuff
e.preventDefault();
e.stopPropagation();
//-or-
//return false;
}
});
there's no event for ENTER you have to check every key and then if evt.keycode == 13 you have an ENTER
Hope this helps
You just need to add some logic to your event handler to check to see which key triggered the event:
$(function(){
$('#txtValue').keyup(function(event){
if(event.which == 13){
// enter was pressed
}
});
});

Categories

Resources