I have a series of horizontal div boxes that I need to add the relevant href to link to the next one with anchorlinks. As they are produced dynamically I need to add the href wi
You could do something like this using .attr():
$("a.next-video").attr('href', function(i) {
return '#post' + (i+2);
});
Since jQuery 1.4+, .attr() takes a function making this very clean (and cheaper to run).
Or if you don't know the post number (e.g. they're just not in numerical sequence), you can get it from the next <div>
, like this:
$("a.next-video").attr('href', function(i) {
return '#' + $(this).parent().next("div[id^='post']").attr('id');
});
$('.next-video').each(function(index) {
$(this).attr('href', '#post' + (index + 2));
});