Create Multi-state Switches From Buttons - jQuery stateButton

File Size: 3.56 KB
Views Total: 1873
Last Update:
Publish Date:
Official Website: Go to website
License: MIT
   
Create Multi-state Switches From Buttons - jQuery stateButton

The stateButton jQuery plugin creates a customizable multi-state toggle button which allows the user to switch between states on click. Ideal for collapse/expand buttons, ladda buttons(inline loading indicators) and more.

How to use it:

1. Insert the JavaScript file jquery.stateButton.js and we're ready to go.

<script src="https://code.jquery.com/jquery-1.12.4.min.js" 
        integrity="sha384-nvAa0+6Qg9clwYCGGPpDQLVpLNn0fRaROjHqs13t4Ggj3Ez50XnGQqc/r8MhnRDZ" 
        crossorigin="anonymous">
</script>
<script src="jquery.stateButton.js"></script>

2. Create a standard button for the toggle switch.

<button id="toggle">Click Me</button>

3. Active the toggle button which switches between collapse/expand states when clicked (Ideal for multi-level tree view).

$('#toggle').stateButton({
  values: [ "Expanded", "Collapsed" ],
  current: "Collapsed"
});

4. Customize the button text and tooltip content.

$('#toggle').stateButton({
  values: [ "Expanded", "Collapsed" ],
  current: "Collapsed",
  text: {
    "expanded" : "Collapse",
    "collapsed": "Expand"
  },
  tooltip: {
    "expanded" : "Collapse all",
    "collapsed": "Expand all"
  },
});

5. Add additional CSS classes to the toggle button.

$('#toggle').stateButton({
  styleClass: {
    "expanded" : "btn-expanded",
    "collapsed": "btn-collapsed"
  },
  inlineStyle: {
    "expanded": {
      "color": "red"
    },
    "collapsed": {
      "color": "green"
    }
  },
});

6. Execute a callback function when you click the toggle button.

$('#toggle').stateButton({
  clickCallback: function(newState, oldState) {
    console.log('button', this);
    console.log('old state', oldState);
    console.log('new state', newState);
  }
});

7. You can also config the toggle buttons via data-OPTION attributes. Great for multiple toggle buttons on the page.

<button id="toggle"
        data-toggle="state"
        data-state-current="collapsed"
        data-state-values="collapsed,expanded">
</button>

This awesome jQuery plugin is developed by aprofetb. For more Advanced Usages, please check the demo page or visit the official website.