Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since you're representing a single control with a true/false state, you really want to use a checkbox as the underlying form element to maintain compatibility with downlevel browsers, screen readers and so on. One approach here is to associate a label control with the checkbox, and then using a combination of CSS and jQuery to make the actual checkbox itself 'invisible', render the label as a button, and modify the label's border property as the checkbox is checked or unchecked.</p> <p>This code works in Chrome, Safari, Opera, Firefox and IE (thanks to a conditional-comment hack since IE treats hidden form elements differently to other browsers). If you submit the enclosing form you get ordinary HTML checkbox behaviour in the resulting form submission.</p> <pre><code> &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head&gt; &lt;title&gt;jQuery Toggle Button &lt;/title&gt; &lt;script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"&gt;&lt;/script&gt; &lt;style type="text/css"&gt; /* Style the label so it looks like a button */ label { border: 2px outset #cccccc; background-color: #cccccc; position: relative; z-index: 3; padding: 4px; } /* CSS to make the checkbox disappear (but remain functional) */ label input { position: absolute; visibility: hidden; } &lt;/style&gt; &lt;!--[if IE]&gt; /* Conditional styles applied in IE only to work around cross-browser bugs */ &lt;style&gt; label input#myCheckbox { visibility: visible; z-index: 2; } &lt;/style&gt; &lt;![endif]--&gt; &lt;script type="text/javascript"&gt; $(function() { $("#toggleCheckbox").click(function() { $(this).closest("label").css({ borderStyle: this.checked ? 'inset' : 'outset' }); }); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form action="http://www.w3schools.com/html/html_form_action.asp" method="get"&gt; &lt;label for="toggleCheckbox"&gt; &lt;input type="checkbox" name="toggled" id="toggleCheckbox" value="1" /&gt; Toggled?&lt;/label&gt; &lt;input type="submit" name="verb" value="Submit Form" /&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload