InfoHeap
Tech
Navigation
  • Home
  • Tutorials
    • CSS tutorials & examples
    • CSS properties
    • Javascript cookbook
    • Linux/Unix Command Line
    • Mac
    • PHP
      • PHP functions online
      • PHP regex
    • WordPress
  • Online Tools
    • Text utilities
    • Online Lint Tools
search

jQuery tutorials

    jQuery Ajax

    • jQuery JSONP
    • jQuery .post()
    • jQuery .load()
    • load script with cache enabled
    • jQuery .ajax() file upload

    jQuery CSS

    • change table cells color based on value
    • check if element is visible in viewport
    • display auto fade out message
    • fitvids.js for automatically resizing videos
    • get class list of an element
    • jQuery toggleClass() examples
    • make a div stick at top on scroll
    • table with alternate color rows

    jQuery Events

    • Javascript/jQuery - disable right click
    • jQuery - text input field - change keyup and paste events
    • trigger click using jQuery or Javascript

    jQuery DOM

    • jQuery html() vs text()
    • Check if an element exists
    • find total DOM elements
    • jQuery encode string to html entities
    • jQuery - read and modify iframe content
    • jQuery - create iframe with content
    • jQuery - find select element selectedIndex, value and text
    • jQuery - get checkbox value and checked state
    • jQuery - get javascript object

    jQuery Cookbook

    • get jQuery version
    • jQuery - add text to existing div
    • jQuery sliding effect - slideUp, slideDown, slideToggle

    jQuery UI

    • jQuery UI make element movable
    • jQuery ui slider and input text box - one way binding
    • jQuery ui slider and input text box - two way binding
     
    • Home
    • > Tutorials
    • > Javascript
    • > jQuery

    jQuery toggleClass() examples

    on Mar 2, 2016

    jQuery toggleClass can be used to toggle a class (add or remove depending upon current state) in an element (or set of matched elements). In case one want to toggle multiple classes, the classes can be separated by space.

    Here are some examples.

    jQuery toggleClass on click

    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
    
    <style type="text/css">
    .red {color: red;}
    .greenbg {background-color: green;}
    </style>
    <h2 class="red">Click to change background color</h2>
    
    <script>
    $("h2").click(function() {
      $(this).toggleClass("greenbg");
    });
    </script>
    refresh done
    try it online

    Use toggleClass to implement a basic menu

    <script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
    <style type="text/css">
    #menu-items {
      position:absolute; background-color:lightgray; margin: 0px;
      display:none;
    }
    #menu-items.active {display:block;}
    </style>
    
    <div id="menu">Click to show/hide menu items</div>
    <ul id="menu-items"><li>item1</li><li>item2</li><li>item3</li></ul>
    <p>This is some random text for the purpose of tutorial.</p2>
    
    <script>
    $( "#menu" ).click(function() {
      $("#menu-items").toggleClass("active");
    });
    </script>
    refresh done
    try it online
    Note that we are using position:absolute for menu-items so that it does not shifts the next element when it becomes visible. You can see more about position at Css position tutorial

    Suggested posts:

    1. CSS general sibling selector
    2. jQuery sliding effect – slideUp, slideDown, slideToggle
    3. jQuery ui slider and input text box – one way binding
    4. Jquery – change table cells color based on value
    5. AngularJS controller basic clock example
    6. Make an element draggable using Vanilla Javascript
    7. Running php eval on code with tags
    8. Javascript – iterate over function arguments
    Share this article: share on facebook share on linkedin tweet this submit to reddit
    Posted in Tutorials | Tagged Javascript, jQuery, jQuery CSS, Tutorials
    • Browse content
    • Article Topics
    • Article archives
    • Contact Us
    Popular Topics: Android Development | AngularJS | Apache | AWS and EC2 | Bash shell scripting | Chrome developer tools | Company results | CSS | CSS cookbook | CSS properties | CSS Pseudo Classes | CSS selectors | CSS3 | CSS3 flexbox | Devops | Git | HTML | HTML5 | Java | Javascript | Javascript cookbook | Javascript DOM | jQuery | Kubernetes | Linux | Linux/Unix Command Line | Mac | Mac Command Line | Mysql | Networking | Node.js | Online Tools | PHP | PHP cookbook | PHP Regex | Python | Python array | Python cookbook | SEO | Site Performance | SSH | Ubuntu Linux | Web Development | Webmaster | Wordpress | Wordpress customization | Wordpress How To | Wordpress Mysql Queries | InfoHeap Money

    Copyright © 2025 InfoHeap.

    Powered by WordPress