javascript - <li> active masterpage jQuery not working -


i got menu on mastepage, below:

<div class="navtop">     <ul class="nav">         <li class=""><a href="default.aspx">home</a></li>         <li class=""><a href="test.aspx">link</a></li>         <li class=""><a href="hello.aspx">link</a></li>         <li class=""><a href="world.aspx">link</a></li>     </ul> </div> 

to set class of <li></li> active using javascript code:

<script>     $("li").click(function () {         alert("this message");         if ($("li").hasclass('active')) {             $("li").removeclass('active');         }         $(this).addclass('active');     }); </script> 

this should work, because saw somewhere , should work. doesn't work me, menu stays empty class="" , alert-message javascript (to test it) doesn't work either.

can me? doing wrong?

check havve added jquery , bind event in document.ready event. code seems work. missed closing quotes here class="navtop

adding jquery

<script src="http://code.jquery.com/jquery-latest.min.js"         type="text/javascript"></script> 

putting code in document.ready

live demo

$(document).ready(function(){    $("li").click(function (event) {       event.preventdefault();             if ($("li").hasclass('active')) {            $("li").removeclass('active');       }       $(this).addclass('active');    }); }); 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

html - Unable to style the color of bullets in a list -

c# - must be a non-abstract type with a public parameterless constructor in redis -