Reputation: 33
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();
Upvotes: 2
Views: 54
Reputation: 1267
I've made a little sample of when the user deselects the checkbox. Hope this is what you're looking for. Goodluck!
EDIT
the function only fires if the user presses c and the checkbox is unchecked if it is checked the function wont fire
$(document).keyup(function(e){
if(!$('#check').is(':checked')) {
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"/>
Upvotes: 0
Reputation: 14348
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"/>
Upvotes: 3
Reputation: 8178
you can check below code
$( "#cHideChat").keypress(function( event ) {
if ( event.which == 99 ) {
$("#cHideChat").click()
//event.preventDefault();
}
});
Upvotes: 0
Reputation: 25527
If you need to check the checkbox, you need to use,
$("#cHideChat").prop("checked",true);
Your code will search for the jquery click event handler. ie it will call the click event handler only if you wrote a click handler using jquery.
If you really want to emulate the click on that checkbox, you need to use the dom element click,
$("#cHideChat")[0].click();
Upvotes: 2