Tuesday, January 18, 2011

Simple way to add class on hover with jQuery

If you need to add a class on hover the best way to do it is with jQuery


<html xmlns="http://www.w3.org/1999/xhtml">

<head>



<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js' type="text/javascript"></script>

<script type="text/javascript">

    $(document).ready(function() {

       $('.myDiv').hover(function() {

  $(this).addClass('newClass');

}, function() {

  $(this).removeClass('newClass');

});

    });  

</script>



</head>



<body>

<div class="myDiv">This is my new Div</div>

</body>

</html>

No comments: