SaladTato - Agario SaladTato - Agario - 6 months ago 8
jQuery Question

how to click a check box from its id with a certain key

I want to make a script that clicks a checkbox when I press the 'c' key. Can somebody show me how to do this or if it's simple, make one?This is what I want it to do.

$("#cHideChat").click();

Answer

You can try something like this. This fires when you press c

$(document).keyup(function(e){
  if(e.which == 67){
    $("#check")[0].click()
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Check<input type="checkbox" id="check"/>

If you want to make it work only if the checkbox is unchecked :

$(document).keyup(function(e){
  if(e.which == 67){
    if($("#check").prop("checked") == false){
    	$("#check")[0].click()
    }
  }
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
Check<input type="checkbox" id="check"/>

Comments