How to compare events in JavaScript - javascript

I want to give document onkeydown & onkeyup in same function
How to know which event that trigger the function ?
<script>
document.onkeydown = listener;
document.onkeyup = listener;
function listener(e) {
if(e === "onkeydown"){
console.log("onkeydown");
}
else if(e === "onkeyup "){
console.log("onkeyup ");
}
else{
console.log("unknown");
}
}
</script>

You can use e.type
document.onkeydown = listener;
document.onkeyup = listener;
function listener(e) {
if(e.type === "keydown"){
console.log("keydown");
}
else if(e.type === "keyup "){
console.log("keyup ");
}
else{
console.log("unknown");
}
}

You can use e.type but remove the on prefix.
function listener(e) {
if(e.type.indexOf("key") !== 1)
console.log("onkey" + (e.type == "keydown" ? "down" : "up"));
else { console.log("unknown"); }
}

You need to check e.type. e is an Event object, not a string.

use this code:
<script>
document.onkeydown = listener;
document.onkeyup = listener;
function listener(e) {
if(e.type === "keydown"){
console.log("onkeydown");
}
else if(e.type === "keyup "){
console.log("onkeyup ");
}
else{
console.log("unknown");
}
}
</script>
e.type return which event is triggered

Related

Check for alt + key js

I want to see if the alt key and j(or any other number or letter) key are being pressed at the same time.
Here's what I have so far:
document.onkeydown = function(e) {
if(e.altKey && e.keyPressed == "j") {
console.log("alt + j pressed")
}
}
This isn't working.
Any help on this?
You should be getting the event's key property instead:
document.onkeydown = function(e) {
if(e.altKey && e.key == "j") {
console.log("alt + j pressed")
}
}
That's because a KeyboardEvent does not have a keyPressed property. There is a key property that indicates which key was pressed.
Maybe it works
var altKeyPressed = false;
document.addEventListener("keydown", function(e) {if(e.altKey) {
altKeyPressed = true;
}});
document.addEventListener("keydown", function(e) {if(e.key === "j" && altKeyPressed) {
console.log("alt + j pressed");
altKeyPressed = false;
}});

Datatable search result add to cart enter key

I have joined a script called jquery.mycart with datatable.
document.onkeydown = function(evt) {
evt = evt || window.event;
var isEscape = false;
if ("key" in evt) {
isEscape = (evt.key == "Enter" || evt.key == "Enter");
} else {
isEscape = (evt.keyCode == 13);
}
if (isEscape) {
table.rows( { search:'applied' } ).data().each(function(value, index) {
console.log(value, index);
});
}
};
I have a problem that when we suppose we search for "Antalgina" when we press enter this search is added to the shopping cart. (look at the console).
Full code
https://codepen.io/anon/pen/oaQJNg
Please how could I do it or give me some hint
I hope you can help me.
Working codePen.
You could a simple jQuery event like :
$("document").on('onkeydown', function (e) {
if (e.keyCode !== 13) {
table.rows( { search:'applied' } ).data().each(function(value, index) {
console.log(value, index);
});
});
});

disable enter key on page, but notin textarea

When i press ENTER key the page disappears. So i am disabling the enter key for the form. But If i am in Textarea, I want to enable enter key for new line. NOT A JQUERY SOLUTION PLEASE.
function stopRKey(evt){
var evt = (evt) ? evt : ((event) ? event : null);
var node = (evt.target) ? evt.target : ((evt.srcElement) ?
evt.srcElement : null);
if ((evt.keyCode == 13) && (node.type=="text")) {
return false;
}
else if((evt.keyCode==13) && (node.type=="checkbox")){
return false;
}
else if((evt.keyCode==13) && (node.type=="table")){
return false;
}
else if((evt.keyCode==13) && (node.type=="file")){
return false;
}
else if((evt.keyCode==13) && (node.type=="paragraph")){
return false;
}
else if((evt.keyCode==13) && (node.type=="textarea")){
return false;
}
else if((evt.keyCode==13) && (node.type=="list")){
return false;
}
else if((evt.keyCode==13) && (node.type=="choice")){
return false;
}
else if((evt.keyCode==13) && (node.type=="date")){
return false;
}
else if((evt.keyCode==13) && (node.type=="ip_cidr")){
return false;
}
else {
return true;
}
};
document.onkeypress = stopRKey;
document.activeElement gives you the currently focused Element.
function stopRKey(ev){
if (ev.which == 13){ // which normalizes charCode and keyCode
var e = document.activeElement;
if (e.type == "textarea") return true; // this can also be e.id or e.classList
else return false;
}
}
document.onkeypress = stopRKey;
<input type="text">
<textarea></textarea>
Here an another solution:
var textAreas=document.getElementsByTagName('textarea');
for(var i=0;i<textAreas.length;i++){
textAreas[i].onkeypress=stopRKey;
}
I'm on mobile so not tested but this should work
document.addEventListener('keypress', e => {
If(document.querySelector(':focus').type !== 'textarea' && e.keyCode == 13) {
e.preventDefault();
}
});
document.onkeypress = function(evt) {
if(evt.target.toString() !== '[object HTMLTextAreaElement]' && evt.charCode === 13){
evt.preventDefault();
return;
}
};

If the user presses the enter key, perform the same action as clicking the submit in Javascript

How do I get my enter key to perform the same action and my submit button?
I have my code here, and I would like the form to submit if the user presses submit, or presses the enter button on the keyboard
I have tried using if statements with keycode 13, but nothing is working for me.
Here is my Javascript code:
window.addEventListener('load', function(){
// Add event listeners
document.getElementById('add-item').addEventListener('click', addItem, false);
document.querySelector('.todo-list').addEventListener('click', toggleCompleted, false);
document.querySelector('.todo-list').addEventListener('click', removeItem, false);
function toggleCompleted(event) {
console.log('=' + event.target.className);
if(event.target.className.indexOf('todo-item') < 0) {
return;
}
console.log(event.target.className.indexOf('completed'));
if(event.target.className.indexOf('completed') > -1) {
console.log(' ' + event.target.className);
event.target.className = event.target.className.replace(' completed', '');
document.getElementById('add-item').value='';
} else {
console.log('-' + event.target.className);
event.target.className += ' completed';
}
}
function addItem() {
var list = document.querySelector('ul.todo-list');
var newItem = document.getElementById('new-item-text').value;
var newListItem = document.createElement('li');
newListItem.className = 'todo-item';
newListItem.innerHTML = newItem + '<span class="remove"></span>';
list.insertBefore(newListItem, document.querySelector('.todo-new'));
document.getElementById('new-item-text').value = "";
}
function removeItem(event) {
if(event.target.className.indexOf('remove') < 0) {
return;
}
var el = event.target.parentNode;
el.parentNode.removeChild(el);
}
function changeTitle() {
var title = prompt("change the title");
}
function handle(e){
if(e.keyCode === 13){
addItem();
}
return false;
}
});
You have to use the keydown event for this like below
document.onkeydown=function(evt){
var keyCode = evt ? (evt.which ? evt.which : evt.keyCode) : event.keyCode;
if(keyCode == 13)
{
//your function call here
}
Try adding this to your set of addEventListeners
document.getElementById('add-item').addEventListener('keydown', handle, false);

get keycode with shiftkey

I want to get keyCode if they are pressed with shift Key,
for ex. I press a and then b. Then I want to store it in one array like this [shiftkey,65,66] please suggest me if this is possible or I am going wrong.
You can try this sanjay:
<input id="down" type="text"/>
var your_array = [];
document.onkeydown = function (e) {
var keyPress;
if (typeof event !== 'undefined') {
keyPress = this.value + String.fromCharCode(e.keyCode);
}
else if (e) {
keyPress = e.which;
}
if(keyPress == '16'){
keyPress = 'shift';
}
your_array.push(keyPress);
alert(your_array);
// returns [shift,65,66];
return false; // Prevents the default action
};
var a = [];
var wait = true;
window.onkeydown = function (e) {
if (wait) {
if (a[0] != e.keyCode) { a[0] = e.keyCode; }
}
wait = false;
window.addEventListener('keyup', key_up, false);
}
function key_up(e) {
a[1] = e.keyCode;
if (a[0] == a[1]) { a = []; }
wait = true;
window.removeEventListener('keyup', key_up, false);
console.log(a);
alert(a);
}
var pressedKeysSeries = [];
$(document).on('keyup', function (e) { pressedKeysSeries = []; });
$(document).on('keydown', function (e) {
if (e.shiftKey && pressedKeysSeries.indexOf(e.key) === -1) {
pressedKeysSeries.push(e.key);
}
console.log(pressedKeysSeries);
});

Categories

Resources