franenos
franenos

Reputation: 357

JS Click event doesn't work

The code:

document.getElementById("theid").onfocus=function fone(){
    document.onkeypress = function(myEvent) {
        var code = myEvent.which;
        if ((code === 13) || (code === 32)) {
            document.querySelector('.someclass').click();
        }
    }
}

I can't get what is wrong with my code...

I'm trying to call the click() when Enter or Space buttons are used while the element in focus.

EDIT

Sorry, I just got too much of learning the code for today, I guess. The case wasn't the click event. The code is correct.

Upvotes: 1

Views: 2619

Answers (1)

Jorge Zuverza
Jorge Zuverza

Reputation: 915

You could use JQuery to get this. Please see the following fiddle:

https://jsfiddle.net/ko842xbp/

document.getElementById("theid").onfocus=function fone(){
    document.onkeypress = function(myEvent) {

        var code = myEvent.which;
        if ((code === 13) || (code === 32)) {

           $('.someclass').click();
        }
    }
}

 $('.someclass').click(function(){

 alert(".someclass was clicked")

 });

Upvotes: 2

Related Questions