jQuery 'Enter' and 'ESC' simulation - javascript

I want to simulate a couple of clicks. A 'Save' and 'Cancel' anchor clicks.
I have this as my enter simulation
$('.group').live('keypress', function(e){
code = e.keyCode ? e.keyCode : e.which;
if(code.toString() == 13){
$(this).find('a.saveChanges').click();
}
});
and this as my esc simulation
$('.group').live('keypress', function(e){
code = e.keyCode ? e.keyCode : e.which;
if(code.toString() == 0){
$(this).find('a.discardChanges').click(function(){
GROUP.find('.group-text')
.text(GROUP.data('origText'))
.end().removeData('origText');
GROUP.find('.groupcount').fadeIn('slow');
GROUP.find('.group-image').fadeIn('slow');
GROUP.removeClass('editmode');
});
}
});
My enter seems to work perfect, but my esc doesn't. I'm running this in Firefox at the moment.

Just use e.which. jQuery normalizes it for you across browsers.
Then test for 27.
EDIT: It also looks like you need to use keyup instead of keypress for some reason with the ESC key.
Example: http://jsfiddle.net/uRE7x/
$('.group').live('keyup', function(e){
if(e.which == '27'){
$(this).find('a.discardChanges').click(function(){
GROUP.find('.group-text')
.text(GROUP.data('origText'))
.end().removeData('origText');
GROUP.find('.groupcount').fadeIn('slow');
GROUP.find('.group-image').fadeIn('slow');
GROUP.removeClass('editmode');
});
}
});

Since you didn't provide any html, I just focused on esc functionality. esc is character 27, probably why your function isn't working.
<html>
<input></input>
<script>
$(document).delegate('input','keypress', function(e){
code = e.keyCode ? e.keyCode : e.which;
if(code=== 27){
alert('esc pressed');
}
});
</script>
</html>

Related

JQuery keypress special buttons issue

I am working about press the key of insert on the my page , but i have any error about keycode of insert.
$(document).on('keypress', function(e) {
var code = e.keyCode || e.which;
console.log(code);
if(code===45){
alert("Insert Clicked");
}
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
this is my code . When i clicked some key it work fine , but when i press insert,tab,del,backspace,shift it doesn't work. There is any mistake ? I didn't find any mistake.
You can use keydown with e.preventDefault();:
$(document).on('keydown', function(e) {
var code = e.keyCode || e.which;
console.log(code);
if(code == 45) {
e.preventDefault();
alert("Insert Clicked");
}
});
Try the following code
$(document).on('keydown', function(e) {
var code = e.keyCode || e.which;
console.log(code);
if(code===45){
alert("Insert Clicked");
}
});
Here is the working jsfiddle: https://jsfiddle.net/t5458erd/

How to capture ALT+C keypress

I want catch an event for Alt+c or something like that. My code is
html
<input type="text" id="name"/>
JavaScript
$("#name").keydown(function(e) {
if(e.keyCode == 67 && e.keyCode == 18){alert(e.keyCode);}
});
where is the problem? How it works on both Chrome & firefox?
You need to check for e.altKey instead:
if(e.altKey && e.keyCode == 67){alert(e.keyCode);}
Basically, you are checking for two codes as the same time. The event (e) has several values you can work with ... including altKey which is a boolean (true or false) ...
Try ... watching the e.altKey and the e.keyCode values.
$("#name").keydown(function(e) {
if(e.altKey && e.keyCode == 67) {
alert(e.keyCode);
}
});
With the right version of jQuery, there should be no issue between browsers.
$(document).keydown(function(e) {
//console.log(e.keyCode); If you want to check other keys code
if(e.keyCode == 67 || e.keyCode == 18){
console.log("alt or c pressed");
}
});
You can work around this to check if the two keys are pressed at the same time. I sujest you to use an aux var set to zero wich increase his value when keydown event triggered and decrease it when keyup.

A default button to an entire webpage to respond to the ENTER key press

Is it possible to set a default button for the ENTER key press for an entire webpage?
I googled and I came across the below code. But I'm not sure of what this line means var keycode = (event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode)); So I thought of posting this question here at stackoverflow.
Thanks.
<script language="javascript">
$(document).ready(function () {
$("input").bind("keydown", function (event) {
var keycode = (event.keyCode ? event.keyCode : (event.which ? event.which : event.charCode));
if (keycode == 13) {
document.getElementById('btn').click();
return false;
} else {
return true;
}
});
});
</script>
Different browsers/devices1 support different properties of obtaining key codes. The ternary expression is the same as:
var keyCode;
if(event.keyCode) // if keyCode is supported get that #top-priority
keyCode = event.keyCode;
else if(event.which) // else, if .which is supported, get that
keyCode = event.which;
else // alas! nothing above is supported
keyCode = event.charCode; // we should take charCode
1 Devices for example EAN barcode reader has a charCode of 13 Since its .keyCode is 0 (falsy), the 1st if condition is failed. Courtesy - MLeFevre
With JQuery (if an option) I would do
$(document).keyup(function(evt) {
if (evt.keyCode == 13) {
// do your thing
}
}
This worked for me in Chrome,FF, Safari and Opera.
Also consider using various checks as in #Gaurang Tandon's answer to cover all hardware specs.

Cannot achieve cross browser compatibility for js function

The code below works for disabling the backspace key in a textarea in Firefox perfectly but not Chrome or Safari, any suggestions would be very much appreciated
$('#texttype').keypress(function(event){
var keycode = (event.keyCode ? event.keyCode : event.which);
if(keycode == '8'){
return false;
}
event.stopPropagation();
});
Why not use e.which, it's normalized in jQuery, and the keycode is an integer.
The keydown event triggers on any key press and gives a keycode in all browsers.
The keypress event triggers after keydown and does give a keycode, but it's only guaranteed for character keys, and does not fire on the backspace key in webkit.
$('#texttype').on('keydown', function(e) {
if ( e.which === 8 ) {
return false;
}
e.stopPropagation();
});
FIDDLE

jQuery not detecting enter key pressed in textarea

My setup: jQuery 1.6.2
I have this HTML
<textarea class="comment_box"> Write a comment...</textarea>
And the following Javascript
<script>
$('.comment_box').keydown(function (e){
if(e.keyCode == 13){
alert('you pressed enter ^_^');
}
})
</script>
When I press the enter key in the textarea, nothing triggers
EDIT Oops, cut and paste error, I do have $ in my code and it still doesn't work, must be something else going on.
My bad, it is a user operator error, it does work. Sorry for the confusion.
$('.comment_box').keypress(function(event) {
// Check the keyCode and if the user pressed Enter (code = 13)
if (event.keyCode == 13) {
alert('you pressed enter ^_^');
}
});
Thats it
Check out this answer:
jQuery Event Keypress: Which key was pressed?
var code = (e.keyCode ? e.keyCode : e.which);
if(code == 13) { //Enter keycode
//Do something
}
For jQuery you need to use the $ to specify.
$('.comment_box').keyd
should do it.

Categories