...

View Full Version : Change Link With Jquery (or javascript)



walker6o9
07-21-2009, 11:48 PM
I'm trying to change one of my link's href properties with jquery, so that when the page loads, it changes the links href from one link to a different link? The goal is to use when link is the user has javascript, and another if they do not.

venegal
07-22-2009, 12:45 AM
Say you have this link here in your HTML:


<a id="link" href="http://originallink">link</a>

You can change the href with


$(document).ready(function(){
$('#link').attr('href', 'http://changedlink');
});

If for some reason you want to change the link, but still want the old link to show in the status bar when hovering, you can do it like this:

$(document).ready(function(){
$('#link').click(function(event){
window.location = 'http://hiddenlink';
event.preventDefault();
});
});

Notice that this is jQuery. In vanilla Javascript it's


window.onload = function () {
document.getElementById('link').href = 'changedlink';
}


or


window.onload = function () {
document.getElementById('link').onclick = function () {
window.location = 'http://hiddenlink';
return false;
}
}

If you already have another onload handler, in the vanilla version you will have to use addEventListener and attachEvent instead of window.onload though.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum