Reputation: 1
I'm trying to figure out if there is a way to make it so that when someone clicks a link (that opens in a new window) it ends up changing the link address on each subsequent click. For example, if someone clicks hyperlink1, it takes them to page1.html on the first go around, page2.html on the second, page3.html on the third.
I've tried to get this to work using jquery and anchorobject.href, but I'm not very well-versed in jquery and have no idea how to progress from that.
Any help would be greatly appreciated. Thanks!
Upvotes: 0
Views: 105
Reputation: 3736
Declare an array that has list of links like it and set new href on click.
var links = new Array('first.html','second.html','third.html');
var i = 0;
$(document).ready(function(){
$('#myhref').attr('href',links[i]);
$('#myhref').click(function(){
//do something like open window.
if(i+1 in links)
{
i++;
}
$(this).attr('href',links[i]);
});
});
Example:
<a href="#" id="myhref">Click here</a>
Open in iframe:
<a href="#" id="myhref" target="myiframe_name">Click here</a>
Use in AJAX:
var links = new Array('first.html','second.html','third.html');
var i = 0;
$(document).ready(function(){
$('#myhref').attr('href',links[i]);
$('#myhref').click(function(){
$("#mydialog").load($(this).attr('href'))
if(i+1 in links)
{
i++;
}
$(this).attr('href',links[i]);
return false;
});
});
Demo on jsfiddle: DEMO
Upvotes: 1
Reputation: 955
$("#link-id").click(function(e){
var oldlink = $(this).attr("href");
var newlink = doSomethingWith(oldlink);
$(this).attr("href",newlink);
});
Hope it helps!
Upvotes: 0