Search code examples
jqueryhashequals-operator

Equal (==) and equal (===) not working for me


I am using if condition in jquery and == is not working for me nor === . :( Here is my code:

var hash = window.location.hash;
var hash = hash.replace('#', ' ');
  alert(hash); /* Returns abc.*/
if( hash === 'abc'){
            alert(hash); /* not coming here.*/
        $('.tab').removeClass('is-active');
    }

Any help? Thanks in advance.


Solution

  • window.location.hash will return #abc not abc. So, Replace below code::

    var hash = window.location.hash;
    

    With this::

    var hash = window.location.hash.split('#')[1];
    

    FULL code will be::

    var hash = window.location.hash.split('#')[1];
    if( hash === 'abc'){
        $('.tab').removeClass('is-active');
    }
    

    It will work.