Tuesday, March 19, 2013

jQuery / adding CSS style dynamically

How to add some css styles dynamically with jQuery?
jQuery comes with css() function to do that.

Example shows how to change dynamically text color.

<html>
<head>
<title>jQuery</title>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<script type="text/javascript">
 
    $(document).ready(function()
    {
        $("#J_action_addCssStyle").click( function()
        {
            $("#mymsg").css("color", "red");
        });        
    });
 
</script>

</head>

<body> 
    
    <a id="J_action_addCssStyle" href="javascript:void(0)" >Show message</a>

    <div id="mymsg">
        This is my text.
    </div>

</body>
</html>

Official documentation of jQuery css is here.

No comments: