How can I hide an element only if it matches a specific hash?

I am trying to hide the form only if it is on the root site or on a specific hash https://www.example.com/#uno . My code below results in the form not showing on all subpages (/ # dos, / # tres, etc.). Can someone help me understand what's going on?

$(function(){
    if (location.hash == "" || location.hash == "#uno"){
      $("#form").hide();
    } else {
      $("#form").show();
    };
}());

      

+3


source to share


1 answer


This is because you hide the form once, but you don't force it to reappear until you reload the page. (You only check the hash once when you load the entire page, not when the hash changes.



function showHideForm() {
    if (location.hash == "" || location.hash == "#uno"){
        $("#form").hide();
    } else {
        $("#form").show();
    };
}

window.onhashchange = showHideForm;

      

+4


source







All Articles