I have the following code which when used in a link goes forward to the next link … so I’m basically using it to do the function of a next button.
What I need to do is to create a reverse version so I can use it as a Back button.
Here is the code:
<script>
$("a.mytrigger").click(function(e){
var index = $(this).attr("href").substr(5);
$('#navigation li:nth-child('+parseInt(index)+') a').click();
e.preventDefault();
});
</script>
UPDATE:
Please see the link below for further details:
Javascript JQuery Script click link by code (Sliding Form Script)
I think your problem can be solved more easily, with less HTML.
have a look at my fiddle.
A little explanation :
I bind with “live” ( you might need to use “on” instead . the syntax might be a bit different)
with the “live” method I attach “click” event on each link. the handler operates on each link according to its “next”/”prev” class I attached from before.
For the “next” class, I use the “next” function in JQuery to get the next “li” element.
For the “prev” class, I use the “prev” function in JQuery to get the previous “li” element.
Please note that before I invoke “prev” or “next” functions, I make sure I reference the “li” object which is wrapping the link. For that I use the “closest(‘li’)” call on $(this)
Let me know if there’s anything more you need.