-1

I want to activate this function,

$(document).keydown(function(e){
    if (e.which == 65){
        $('#test').click();
    }
});

when this checkbook is checked.

<input type="checkbox" id="keyboard">

Sorry if this question is already exist.

rrk
  • 15,677
  • 4
  • 29
  • 45

2 Answers2

5

you can use this.

$("#keyboard").change(function() {
  if ($(this).is(":checked")){
    //your function goes here
  }
});
htoniv
  • 1,658
  • 21
  • 40
1

Check is your checkbox checked inside that function. For example, one of way is to do it this way

$(document).keydown(function(e){
    if (e.which == 65 && $('#keyboard:checked').length > 0){
        $('#test').click();
    }
});

This statement

$('#keyboard:checked').length > 0

checks whether there is more than 0 elements with id keyboard that are checked. You can also check using == 1. Other way is

$('#keyboard').prop('checked') == true

this is written in different manner, but works all the same.