javascript - Add class if hash value exists in page URL -


i'm trying add class div if hash value exists in url:

<script type="text/javascript">        if(window.location.hash) {            alert(window.location.hash);            $(".pinit").addclass("pinned");            $(".hideit").addclass("hidden");            $(".changeit").addclass("changed");        } else {            $(".pinit").removeclass("pinned");            $(".hideit").removeclass("hidden");            $(".changeit").removeclass("changed");        }    </script> 

html:

<header class="changeit">   </header>     <div class="heading">     <h1 class="hideit">test</h1>     <h2 class="pinit">     </h2>   </div> 

my script show alert not add class. problem?

the problem code being run before pinit etc exist. need wrap it:

$(function(){     if(window.location.hash) {         alert(window.location.hash);         $(".pinit").addclass("pinned");         $(".hideit").addclass("hidden");         $(".changeit").addclass("changed");     } else {         $(".pinit").removeclass("pinned");         $(".hideit").removeclass("hidden");         $(".changeit").removeclass("changed");     } }); 

more info jquery here


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -