Reversing jQuery code function

Hi all,

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>

Please see this link... http://tympanus.net/Tutorials/FancySlidingForm/

If you click on the tabs at the bottom ... The next tab is the code above what I need to do it the reverse ... in other words go back.

THanks
error77Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chaitu chaituCommented:
i check your code.i think its working as per your expectin i believe.

if you want to trigger the function already existed;
use trigger function.

$("a.mytrigger").trigger('click');
0
Michel PlungjanIT ExpertCommented:
You mean

<script>
    $("a.mytrigger").click(function(e){  
        var index = parseInt($(this).attr("href").substr(5));  
       if ($(this).attr("id")==="next") {
         index++;
         if (index>maxIndex) index=0; //wrap
       }
       else {
         index--;
         if (index<0) index=maxIndex; //wrap
       }
        $('#navigation li:nth-child('+index+') a').click();  
        e.preventDefault();  
    });                        
</script>
0
error77Author Commented:
So if I am using this:

$("a.mytrigger").click(function(e){  
var index = $(this).attr("href").substr(5);  
$('#navigation li:nth-child('+parseInt(index)+') a').click();  
e.preventDefault();  
});

to go foward ... what's the script to go back?

Here's my currebt one ..

$("a.back_mytrigger").click(function(e){  
var index = $(this).attr("href").substr(5);  
$('#navigation li:nth-child('+parseInt(index)-') a').click();  
e.preventDefault();  
});

What do I change in it?

Thanks


0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Michel PlungjanIT ExpertCommented:
Mine should do both depending on the ID

They both have class="mytrigger" and next has id="next" - the other can have any ID
0
error77Author Commented:
For the NEXT... would I be able to change it to a class because I need to use several next button as I have multiple steps ?
0
Michel PlungjanIT ExpertCommented:
Yes
0
error77Author Commented:
OK, I'm trying the code but cannot get it to work...

This is what I'm doing to trigger the buttons:


<a href="#step2" class="mytrigger>Go Back</a>

<a href="#step3" class="mytrigger next>Go Next</a>

is this the right way?
0
Michel PlungjanIT ExpertCommented:
I'll look in a little while
0
error77Author Commented:
Thanks a lot!
0
Michel PlungjanIT ExpertCommented:
If your html is this
<a href="#step2" class="mytrigger">Go Back</a>

<a href="#step3" class="mytrigger">Go Next</a>

and your code (nth-child even counts from 1)

<script>
    $("a.mytrigger").click(function(e){  
        var index = $(this).attr("href").substr(5);  
        $('#navigation li:nth-child('+parseInt(index)+') a').click();
        e.preventDefault();  
    });                        
</script>

then I do not see what you need to change... Like chaituu said

Perhaps you can show your page and tell us what happens when you run the code?
0
Michel PlungjanIT ExpertCommented:
you may need to change
var index = $(this).attr("href").substr(5);  
to
var hash = $(this).attr("href").split("#")[1];
var index = hash.replace("step","");  
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
b1xml2Commented:
Recommend points awarded to mplungjan
0
Michel PlungjanIT ExpertCommented:
We will first need to see it working and then likely a split with chaituu since I was barking up the wrong tree with my first post(s)
0
error77Author Commented:
Sorry that didn't work :o/

Can't it be modified so I can tell it - Go to a specific ID and have it go there?

0
error77Author Commented:
OK That worked. My mistake :)  Thanks a lot!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.