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

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -