jQuery Method How to - Use mouseover() to change the color of any paragraph to red on mouseover event








Question

We would like to know how to use mouseover() to change the color of any paragraph to red on mouseover event.

Answer


<!--from ww w .j  a  v  a 2s.c o  m-->
<html>
  <head>
    <script type="text/javascript" src='http://code.jquery.com/jquery-1.5.2.js'></script>
    <script type="text/javascript">
        $(document).ready(function(){
                    $("p").mouseover(function () {
                         $(this).css("color","red");
                    });
        });
    </script>
  </head>
  <body>
      <p>asdf</p>
  </body>
</html>

The code above is rendered as follows: