jquery wizard adds hash even on error

I have a jquery tabbed wizard and i want to control the url when someone goes next so they can use the back button on their browser. I found this script which works fine. But once I added validation, the validation runs onnext function. so the order happens, the person clicks next, the hash is added to the url, then it validates and if the error occurs, how do I tell it to remove the hash?
    if(location.hash) {
        $('#donationWizard a[href=' + location.hash + ']').tab('show');
    }   


  // add a hash to the URL when the user clicks on a tab
  $('a[data-toggle="tab"]').on('click', function(e) {
    history.pushState(null, null, $(this).attr('href'));
  });
  // navigate to a tab when the history changes
  window.addEventListener("popstate", function(e) {

 var anchor = location.hash || $("#donationWizard a[data-toggle=tab]").first().attr("href");
    $('#donationWizard a[href=' + anchor + ']').tab('show');
  });
});

Open in new window

This is my validation script

$('#donationWizard').bootstrapWizard({
        'tabClass': 'nav-pills2',
        'onNext': function(tab, navigation, index) {
            var $valid = $("#form1").valid();
                if(!$valid) {
                    $validator.focusInvalid();
                    return false;
                }
            }
        });  //end wizard

Open in new window

rivkamakAsked:
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.

Alexandre SimõesManager / Technology SpecialistCommented:
In my opinion you should get rid of the link onclick event and move the history.pushState code into the 'onNext' wizard event, executing it if the validation passed.

I see that the event has a tab argument so I think you'll be able to mimic the functionality in there.
0
rivkamakAuthor Commented:
that sounds great.
can you show me how to code the validation script to incorporate that?
0
Alexandre SimõesManager / Technology SpecialistCommented:
Here you go (don't forget to get rid of the click event handler):
$('#donationWizard').bootstrapWizard({
    'tabClass': 'nav-pills2',
        'onNext': function (tab, navigation, index) {
        var $valid = $("#form1").valid();
        if (!$valid) {
            $validator.focusInvalid();
            return false;
        } else {
            var link = $(tab[0]).find('a').attr('href');
            history.pushState(null, null, link);
        }
    }
}); //end wizard

Open in new window


Also note that instead of a pushState you can just change the hash, letting the browser manage history stack by himself:
window.location.hash = link;

Open in new window

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
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.