Mehdi Hadjar
Mehdi Hadjar

Reputation: 675

Use Jquery trigger method for links

Here is my sample http://jsfiddle.net/RaQtg/4/ I want to redirect my page to another URL by pressing G key, But I couden't trigger click function.

any ideas?

thanks

Upvotes: 0

Views: 73

Answers (3)

Dominic Green
Dominic Green

Reputation: 10258

This seems to work for the key g

$(document).on('keydown', function(evt) {

    if (evt.keyCode == 71) //G key is pressed
    {
        window.location = "http://"+$('.mylink').attr("href");

    }

});

Example here http://jsfiddle.net/yDsC8/

Upvotes: 1

Fabrizio Calderan
Fabrizio Calderan

Reputation: 123397

.trigger() like .click() can only work if you defined an event handler using .bind()(or .live()or .on() and all shortcuts available) methods.

try instead

$(document).on('keydown', function(e) {
    if (e.keyCode === 103) //G key is pressed
    {
       alert("g key is pressed");
       location.href = $('mylink').attr('href');
    }
});

Upvotes: 1

Dutchie432
Dutchie432

Reputation: 29160

You could try something like:

window.location = $('.mylink').attr('href');

If you have more than one item with the class mylink, the first one will always be used

Upvotes: 3

Related Questions