2

I am trying to get a button class to be clicked and redirected to a new page.

But it is not working for me.

Here is my code:

    $('li#menu-item-28').on('click', function(event) {
    event.preventDefault();
        //alert('Clicked');
    url = "http://wwww.bbc.co.uk";
        $(location).attr("href", url);
    });

Any help would be appreciated :)

3
  • If you are trying to redirect directly in this handler you should write window.location.href = url Commented May 30, 2017 at 13:06
  • It does not work because location is not an element in the DOM Commented May 30, 2017 at 13:07
  • put single quotes around location, if it is a class put a dot also, if it is an id put # Commented May 30, 2017 at 13:08

4 Answers 4

2

try window.location.href = url;

$(document).on('click','li#menu-item-28',function(event) {
    event.preventDefault();
    url = "http://www.bbc.co.uk";
    window.location.href = url;
});
Sign up to request clarification or add additional context in comments.

1 Comment

Hey Dinesh - thanks for this but this doesn't work for me :)
1

Read this link below:

window.location.href

 $('li#menu-item-28').on('click', function(event) {
    event.preventDefault();
        //alert('Clicked');
    window.location.href = "http://www.bbc.co.uk";

    });

3 Comments

You got a typo.
windoe.location.href = "wwww.bbc.co.uk"; should be: window.location.href = "wwww.bbc.co.uk";
Hey Prasad, just edited this. Thanks for the solution mate. I will mark as the answer. Cheers for the help have a good day.
0

if your element generate dynamically , you should listen on document :

$(document).on('click','li#menu-item-28',function(){
      url = "http://wwww.bbc.co.uk";
       window.location.href = url;
});

Comments

0

I use this code:

url = "http://wwww.bbc.co.uk";
window.open(url, '_blank');

works for me

Comments

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.