Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This looks about as simple as you can get for forcing only one button to have the "selected" state. If you need to do this for many elements, see my code below.</p> <p>Toggle should work on iDevices as long as the jQuery library you included supports it. However, you will not get a hover effect on iDevices since there is no mouse.</p> <p><strong>Code Example:</strong></p> <p>If you plan to do this frequently with buttons (rocker switches) where only one element can have the "selected" state you could make a function like this:</p> <p><strong>CodePen</strong>: <a href="http://codepen.io/Skrypt/pen/dyCha" rel="nofollow">http://codepen.io/Skrypt/pen/dyCha</a></p> <p><strong>HTML</strong></p> <pre><code>&lt;div class="rockerSwitch myRocker1"&gt; &lt;button class="left"&gt;On&lt;/button&gt;&lt;button class="right"&gt;Off&lt;/button&gt; &lt;/div&gt; &lt;div class="rockerSwitch myRocker2"&gt; &lt;button class="left"&gt;True&lt;/button&gt;&lt;button class="right"&gt;False&lt;/button&gt; &lt;/div&gt; &lt;div class="rockerSwitch myRocker3"&gt; &lt;button class="left"&gt;Option 1&lt;/button&gt;&lt;button class="right"&gt;Option 2&lt;/button&gt; &lt;/div&gt; </code></pre> <p><strong>CSS</strong></p> <pre><code>.rockerSwitch button { background-color: #dcffb2; border: 1px solid #87cf30; cursor: pointer; outline: 0; } .rockerSwitch button.left { margin-right: 0px; border-radius: 5px 0px 0px 5px; } .rockerSwitch button.right { margin-left: 0px; border-radius: 0px 5px 5px 0px; } .rockerSwitch button:hover { background-color: #fff; } .rockerSwitch button.selected { background-color: #87cf30; } </code></pre> <p><strong>JS/jQuery</strong></p> <pre><code>$(function() { $('.myRocker1').rockerSwitch(); $('.myRocker2').rockerSwitch(); $('.myRocker3').rockerSwitch(); }); $.fn.rockerSwitch = function() { var left = $('.left', this); var right = $('.right', this); left.on('click', function() { left.addClass("selected"); right.removeClass("selected"); }); right.on('click', function() { right.addClass("selected"); left.removeClass("selected"); }); } </code></pre>
    singulars
    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. 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