-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.

3
  • 3
    Activating which function? Commented Feb 27, 2016 at 18:04
  • You can look at this stackoverflow.com/questions/6358673/… and model your code like that. Commented Feb 27, 2016 at 18:06
  • Please explain in more detail what you are wanting to have happen Commented Feb 27, 2016 at 18:08

2 Answers 2

5

you can use this.

$("#keyboard").change(function() {
  if ($(this).is(":checked")){
    //your function goes here
  }
});
Sign up to request clarification or add additional context in comments.

1 Comment

you need to detail your answer, in order to educate the OP.
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.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.