jQuery Toggleclass Function Example

Let's have a closer look at the jQuery .toggleclass() function.

Demo Code Download


The $.toggleClass() function is a huge time saver when it comes to have a state either on or off with CSS. The following example sets event handlers for "mouseenter" (the CSS class "img-hover" will be applied to the img) and and "mouseleave" (CSS class removed). Lets have a look at an example of the toggleclass jQuery function in action. Further Reading »


jQuery Function Demo - jQuery4u.com

Example: Toggling a CSS class on an element.

Run Demo View Code


The download package includes all the inline HTML, CSS, JavaScript, jQuery and images required for the .toggleclass() function demo so you can try it yourself and change as you please.

The full code listing for all jQuery functions have been now added to GitHub, you can get them there, we are making them work with the latest versions of jQuery!


<!-- Copyright 2011 jQuery4u.com -->
<!DOCTYPE html>
<title>jQuery Function Demo - jQuery4u.com</title>
    <script src="http://www.jquery4u.com/function-demos/js/jquery-1.6.4.min.js"></script>
    <script src="http://www.jquery4u.com/scripts/function-demos-script.js"></script>

    <script type="text/javascript">

    var JQFUNCS =
            /* ------------------------------ toggleClass Demo ------------------------------ */
                run: function(id)
                    .live('mouseenter', function(e)

                reset: function(id)


    <style type="text/css" media="screen">
        .twittreyesclosed {
            background: url(images/twitter-eyes-closed.jpg) repeat;
        .twittreyesopen {
            background: url(images/twitter-eyes-open.jpg) repeat;


<h3>Example: Toggling a CSS class on an element.</h3>
<p><a href="#" id="toggleclass-demobtn" class="demobtn btn actionr">Run Demo</a> <a href="#" id="toggleclass-codebtn" class="codebtn btn actionr">View Code</a> <a href="#" id="toggleclass-resetbtn" class="resetbtn btn actionr" style="display: none;">Reset</a></p>
<div class="demoarea twittreyesclosed" id="toggleclass" style="width:700px;height:40px;background-color:#002D46;padding:10px;"></div>

<!-- Copyright 2011 jQuery4u.com -->