0

I am trying to create a single variable that contains all of the function key codes. If I remove the brackets and use a single keycode everything works.

$('input').keyup(function (e) {

    var functionKeysPressed = e.which == [114, 115, 116, etc];

    if (!functionKeysPressed) {

    }

});

3 Answers 3

4

Use indexOf()

$('input').keyup(function (e) {

    var functionKeysPressed = [114, 115, 116, etc].indexOf(e.which) > -1;

    if (!functionKeysPressed) {

    }

});
Sign up to request clarification or add additional context in comments.

Comments

0

To check if element exist in array you should use indexOf

function contains(e) {    
    return [114, 115, 116, etc].indexOf(e.which) != -1;
}

Comments

0

Use indexOf to see if that is existing:

[114, 115, 116, etc].indexOf(e.which) != -1

So your code becomes:

$('input').keyup(function (e) {
    var functionKeysPressed = [114, 115, 116, etc].indexOf(e.which) != -1;
    if (!functionKeysPressed) {

    }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.