How to change pagination using specified keyboard keys using javascript

Asked

Viewed 31 times

0

How can I execute page change event using javascript so that when clicking on the arrow keys (< or >), run href of the links with ids Prox-pg and ant-pg, shown below.

<a href="url da pagina anterior" id="ant-pg"> << </a>
<a href="url da proxima pagina" id="prox-pg"> >> </a>

1 answer

1


Use the "keydown" event. Press the key and see if it was the desired key:

// associo o evento keydown
document.addEventListener('keydown', function (event) {
  // se for a tecla <, "clica" o elemento "ant-pg"
  if (event.key === '<') {
    console.log("pressinaoda \<");
    document.getElementById('ant-pg').click();
  }
  // se for a tecla >, "clica" o elemento "prox-pg"
  if (event.key === '>') {
    console.log("pressionada \>");
    document.getElementById('prox-pg').click();
  }
});


function clicou(link) {
   alert("pressionado: " + link.id);
}
<h2>Clique nessa janela para testar</h2>

<a href="#" id="ant-pg" onclick="clicou(this)"> << </a>
<a href="#" id="prox-pg" onclick="clicou(this)"> >> </a>

  • I ran this example that you passed, more when using the keyboard keys < or > this not worked.

  • Here worked, see . You pressed the shift?

  • @Striffer which your browser? can be something like this, see what I used javascript pure, the event.key may be different

Browser other questions tagged

You are not signed in. Login or sign up in order to post.