LetsSeo LetsSeo - 1 year ago 166
Javascript Question

jQuery hotkeys works only once

I have 4 radio input.

And I have assigned them hotkeys.
(Hotkeys are 1,2,3 and 4)

Here is my html code:

<input type="radio" value="2" id="1" name="1">[c2]
<input type="radio" value="4" id="2" name="1">[c4]
<input type="radio" value="1" id="3" name="1">[c1]
<input type="radio" value="3" id="4" name="1">[c3]

and javascript code:

$(document).keypress(function(e) {

if(e.charCode == 49) {

if(e.charCode == 50) {

if(e.charCode == 51) {

if(e.charCode == 52) {

You can also see, I put
events to script just to observe.

The strange thing for me, I can select redio buttons by their hotkeys only once per radio button.


If I press 1, it chooses the correct checkbox and gives me the correct console log.

Then I press 2, and works the same.

But then if I press 1 again, it gives me the console log but hotkey doesn't work.

Here is jsfiddle:

How can I make hotkeys work more than once in one session?

Answer Source

You may have to use $("input:radio[id=2]").prop("checked", true)

Try it on https://jsfiddle.net/f07evno0/1/

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download