Link to home
Start Free TrialLog in
Avatar of error77
error77

asked on

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
Avatar of chaitu chaitu
chaitu chaitu
Flag of India image

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');
Avatar of Michel Plungjan
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>
Avatar of error77
error77

ASKER

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


Mine should do both depending on the ID

They both have class="mytrigger" and next has id="next" - the other can have any ID
Avatar of error77

ASKER

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 ?
Avatar of error77

ASKER

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?
I'll look in a little while
Avatar of error77

ASKER

Thanks a lot!
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?
ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Recommend points awarded to mplungjan
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)
Avatar of error77

ASKER

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?

Avatar of error77

ASKER

OK That worked. My mistake :)  Thanks a lot!