0

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

3 Answers 3

3

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

Sign up to request clarification or add additional context in comments.

Comments

1

.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');
    }
});

Comments

1

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/

2 Comments

It works fine, thanks but I wonder whats wrong with trigger function?
.trigger() triggers a specific event bound to the link. If you do not have a function bound to an item's keyup event, you cannot trigger said keyup event..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.