Show the correct active tab based on the href attribute is not working properly

Show the correct active tab based on the href attribute is not working properly

I have this jquery to give the class active to a tab when the url has the correct hash.

This is working fine with the code below, then the user accesses for example " http://proj.test/user/profile?user=1#myTickets", the #myTickets tab becomes active.

But I want to have this dynamic because there are more tabs, for example, there is a tab #editPassword", and when " http://proj.test/user/profile?user=1#editPassword" is accessed the tab #editPassword should become active.

So the code is working if is just for the #myTickets like:
  
  1. var path = window.location.href;

  2. $('.registration_form_list a').each(function () {
  3. var hash = $(this).attr('href').split('#')[1];

  4. if (this.href === path) {

  5. $('.registration_form_list a').removeClass('active');
  6. $('a[href="#myTickets"]').addClass('active');
  7. }
  8. });
But I want to have this dynamic, so I change to:

  
  1. var path = window.location.href;

  2. $('.registration_form_list a').each(function () {
  3. var hash = $(this).attr('href').split('#')[1];

  4. if (this.href === path) {

  5. $('.registration_form_list a').removeClass('active');
  6. $('a[href="# '+hash+'+).addClass('active');
  7. }
  8. });
But like that is not working, even when  " http://proj.test/user/profile?user=1#myTickets" is acessed the myTickets tab don't become active.

Do you know why?