Evan
Evan

Reputation: 3501

dynamically change the window.location + ID jquery

I'm trying to pull the URL from the link and assign it to the window.location and it's not working. I know I'm close, what am I doing wrong?

JavaScript:

$(document).ready(function () {
    $("#mypage").attr("href", $("#redirectme").attr("href") );
    window.location + "#redirectme";
});

HTML:

<a href="http://www.google.com" id="mypage">My link</a>

Upvotes: 0

Views: 1497

Answers (2)

Paul
Paul

Reputation: 141839

You need to use assignment (=) instead of addition (+):

window.location = $("#mypage").prop("href");

Demo (Waits 5 seconds, so you can see it, and then redirects)

Upvotes: 2

emre nevayeshirazi
emre nevayeshirazi

Reputation: 19241

$(document).ready(function () {  
    window.location = ("#mypage").attr("href") + "#redirectme";
});

<a href="http://www.google.com" id="mypage">My link</a>

Upvotes: 0

Related Questions