I have the following button on my site:
<button onclick='document.location="google.com.br"'>FINALIZAR</button>
How do I get it to click when I press f2? I wanted to do this using jquery.
Can anyone help me?
I have the following button on my site:
<button onclick='document.location="google.com.br"'>FINALIZAR</button>
How do I get it to click when I press f2? I wanted to do this using jquery.
Can anyone help me?
Use the keyup event and check that the keycode is 113 (F2), then just invoke the event of the element you want.
$(document).on('keyup', function(e) {
var keyCode = e.keyCode || e.which;
if (keyCode === 113)
$('#elemento').trigger('click');
});
$('#elemento').on('click', function () {
console.log('Disparado evento');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><ahref="javascript:void(0)" id="elemento" >Exemplo</a>
You can identify the click event on the body and if f2 clicks the button.
$('body').keypress(function(e) {
var code = e.keyCode || e.which;
if (code == 113) { // 113 = f2
$('#btn').click();
}
});
function finalizar() {
console.log("Fui clicado");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><buttonid="btn" onclick="finalizar()">FINALIZAR</a>
You can also do this:
document.onkeyup = KeyCheck;
function KeyCheck(e)
{
var tecla = (window.event) ? event.keyCode : e.keyCode;
if (tecla == 113) {
alert('Pressionou F2') // Aqui você coloca seu clique no botão
}
}
Follow this solution:
<button id="finalizar" onclick='document.location="google.com.br"'>FINALIZAR</button>
$(window).on("keyup", function(event) {
if (event.keyCode == 113) {
$("#finalizar").trigger("click");
}
});