Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you create a toggle button?

Tags:

html

jquery

css

I want to create a toggle button in html using css. I want it so that when you click on it , it stays pushed in and than when you click it on it again it pops out.

If theres no way of doing it just using css. Is there a way to do it using jQuery?

like image 561
Donny V. Avatar asked Nov 21 '08 15:11

Donny V.


People also ask

How do I code a toggle button in HTML?

We can do that by using the HTML label tag and HTML input type = checkbox. HTML code: The HTML code is used to create a structure of toggle switch. Since it does not contain CSS so it is just a simple structure.

What is an example of a toggle button?

In Android, ToggleButton is used to display checked and unchecked state of a button. ToggleButton basically an off/on button with a light indicator which indicate the current state of toggle button. The most simple example of ToggleButton is doing on/off in sound, Bluetooth, wifi, hotspot etc.


2 Answers

The good semantic way would be to use a checkbox, and then style it in different ways if it is checked or not. But there are no good ways do to it. You have to add extra span, extra div, and, for a really nice look, add some javascript.

So the best solution is to use a small jQuery function and two background images for styling the two different statuses of the button. Example with an up/down effect given by borders:

$(document).ready(function() {    $('a#button').click(function() {      $(this).toggleClass("down");    });  });
a {    background: #ccc;    cursor: pointer;    border-top: solid 2px #eaeaea;    border-left: solid 2px #eaeaea;    border-bottom: solid 2px #777;    border-right: solid 2px #777;    padding: 5px 5px;  }    a.down {    background: #bbb;    border-top: solid 2px #777;    border-left: solid 2px #777;    border-bottom: solid 2px #eaeaea;    border-right: solid 2px #eaeaea;  }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>  <a id="button" title="button">Press Me</a>

Obviously, you can add background images that represent button up and button down, and make the background color transparent.

like image 84
alexmeia Avatar answered Sep 30 '22 11:09

alexmeia


JQuery UI makes light work out of creating toggle buttons. Just put this

<label for="myToggleButton">my toggle button caption</label> <input type="checkbox" id="myToggleButton" /> 

on your page and then in your body onLoad or your $.ready() (or some object literals init() function if your building an ajax site..) drop some JQuery like so:

$("#myToggleButton").button() 

thats it. (don't forget the < label for=...> because JQueryUI uses that for the body of the toggle button..)

From there you just work with it like any other input="checkbox" because that is what the underlying control still is but JQuery UI just skins it to look like a pretty toggle button on screen.

like image 44
bkdraper Avatar answered Sep 30 '22 09:09

bkdraper