2

I'm trying to write a clean script to add and remove classes based on the url because I'm using some form of jquery slider plugin.

so I have 5 spans, the first 1 has the active class on pageload

<span class="one active"></span>
<span class="two"></span>
<span class="three"></span>
<span class="four"></span>
<span class="five"></span>

When I click the next button the jquery plugin appends a '#/1' to the end of the url and for every slide the number will go up by one, so this is what I've done so far for each slide:

$(function(){

$('#continue').click(function(){
  var location = window.location.href;

    if (location.indexOf('#/1') > -1 ) {
      $('.one').removeClass('active');
      $('.two').addClass('active');
    }

    if (location.indexOf('#/2') > -1 ) {
      $('.two').removeClass('active');
      $('.three').addClass('active');
    }
  }); 
});

So the problems I'm facing are:

  1. This is only on click function on the next button, if the user clicks the previous button, the active class needs to change to the appropriate number too, but I'll probably need a way of checking exactly which step they are clicking the previous button from and would need to duplicate that code 5 times because there are 5 steps.

  2. If the user refreshes the page span .one will be active again and the step that the user was on will show on the page (it doesn't go back to the first step on refresh)

Any suggestions?

Thanks.

1 Answer 1

1
jQuery(window).hashchange( function(){
    jQuery(location.hash).click();
});

jQuery(window).load( function(){
    jQuery(location.hash).click();
});

these two functions will solve your problem

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.