Shrewd Shrewd - 8 days ago 6
Javascript Question

Multi Submit button and enter key

i have 4 submit button,

<input type="submit" name="zmianapoz" value="Zmiana Ilości" id="dodaj1" />
<input type="submit" name="usunpoz" value="Usunięto" id="dodaj3" />
<input type="submit" value="Pobranie" id="dodaj2" />


and this is fourth:

<input type="submit" name="submit" value="Aktualizuj">


how can i do that if i press enter key=13 then work button fourth not first on web?
i try with function onclick but probably i do something wrong

Answer

You should be able to trap the keydown event and trigger it yourself like so:

document.addEventListener("keydown", function(event) {
  var enter = 13;
  if (event.keyCode === enter) {
    event.preventDefault();
    document.getElementById("dodaj4").click();
  }
});
document.getElementById("dodaj4").addEventListener("click", function() {
  alert("input 4 clicked!");
});
<input type="submit" name="zmianapoz" value="Zmiana Ilości" id="dodaj1" />
<input type="submit" name="usunpoz" value="Usunięto" id="dodaj3" />
<input type="submit" value="Pobranie" id="dodaj2" />

<input type="submit" name="submit" value="Aktualizuj" id="dodaj4">