I would be inclined to use a class in your css that alters the border style or border width when the button is depressed, so it gives the appearance of a toggle button.
You could use an anchor element (<a></a>), and use a:active and a:link to change the background image to toggle on or off. Just a thought.
Edit: The above method doesn't work too well for toggle. But you don't need to use jquery. Write a simple onClick javascript function for the element, which changes the background image appropriately to make it look like the button is pressed, and set some flag. Then on next click, image and flag is is reverted. Like so
var flag = 0;
function toggle(){
if(flag==0){
document.getElementById("toggleDiv").style.backgroundImage="path/to/img/img1.gif";
flag=1;
}
else if(flag==1){
document.getElementById("toggleDiv").style.backgroundImage="path/to/img/img2.gif";
flag=0;
}
}
And the html like so
<div id="toggleDiv" onclick="toggle()">Some thing</div>
If you want a proper button then you'll need some javascript. Something like this (needs some work on the styling but you get the gist). Wouldn't bother using jquery for something so trivial to be honest.
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:
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.
As far as I was searching for answer too, and wanted to acomplish it with CSS. I found solution by CSS NINJA
It is a nice impelmentation of <input type="checkbox"> and some css
Live demo!
Although it is not working in IE 8 you could implement selectivizr! and fix CSS where uses opacity to filter to make it work in IE.
EDIT 2014:
for new toggle buttons I do use solution found on Lea Verou blog visually similar to iOS checkbox
Here is one of the example/variant (more detail described) of ToggleButton using jQuery with <label for="input"> implementation.
1st we will create container for our ToggleButton using classic HTML <input> and <label>
<span>
<input type="checkbox" value="1" name="some_feature_to_select" id="feature_cb" style="display: none;"> <!-- We can hide checkbox bec. we want <label> to be a ToggleButton, so we don't need to show it. It is used as our value holder -->
<label for="feature_cb" id="label_for_some_feature">
<img alt="Stylish image" src="/images/icons/feature.png">
</label>
</span>
Next we will define function for toggling our button. Our button actually is the usual <label> which we will be styling to represent value toggling.
function toggleButton(button) {
var _for = button.getAttribute('for'); // defining for which element this label is (suppose element is a checkbox (bec. we are making ToggleButton ;) )
var _toggleID = 'input#'+_for; // composing selector ID string to select our toggle element (checkbox)
var _toggle = $( _toggleID ); // selecting checkbox to work on
var isChecked = !_toggle.is(':checked'); // defining the state with negation bec. change value event will have place later, so we negating current state to retrieve inverse (next).
if (isChecked)
$(button).addClass('SelectedButtonClass'); // if it is checked -> adding nice class to our button (<label> in our case) to show that value was toggled
else
$(button).removeClass('SelectedButtonClass'); // if value (or feature) was unselected by clicking the button (<label>) -> removing .SelectedButtonClass (or simply removing all classes from element)
}
Function is implemented in a reusable way. You can use it for more than one, two or even three ToggleButtons you've created.
... and finally ... to make it work as expected, we should bind toggle function to an event ("change" event) of our improvised <label> button (it will be click event bec. we are not altering the checkbox directly, so no change event can be fired for <label>).
$(document).ready(function(){
$("#some_feature_label").click(function () {
toggleButton(this); // call function with transmitting instance of a clicked label and let the script decide what was toggled and what to do with it
});
$("#some_other_feature_label").click(function () {
toggleButton(this); // doing the same for any other feature we want to represent in a way of ToggleButton
});
});
With CSS we can define backgorund-image or some border to represent the change in value whilst <label> will do the job for us in altering the value of a checkbox ;).
<input type="checkbox" id="mycbid">
<label for="mycbid" id="mylabelid">Text of the label</label>
Explication:
Under the hood the checkbox will not be displayed but still it can be set as checked or unchecked so it will be possible to use it more or less as usual.
When the checkbox is checked its status "triggers" the css rules under #mycbid:checked + #mylabelid and the label will be styled accordingly with it. This css selector is in fact the key of everything: in fact it selects the sibling of the checkbox (that is the label it self). The label redirects the click to the checkbox thanks to the for attribute, even if the checkbox is not shown.
When the checkbox is not checked its status "triggers" the css rules under #mylabelid and the label will be styled accordingly with it.
Of course it is possible to style the label in may different ways.