Note that there are some explanatory texts on larger screens.

plurals
  1. POCSS change active select for radio
    primarykey
    data
    text
    <p>I'm using the following to create a toggle switch instead of radio buttons - it works great </p> <pre><code>.switch { display: block; float: left; -moz-border-radius: 6px; -webkit-border-radius: 6px; border-radius: 6px; font-weight: bold; text-transform: uppercase; background: #eee; border: 1px solid #aaa; padding: 2px; box-shadow: inset 0 2px 5px rgba(0,0,0,0.2); margin-left: 20px; } .switch input[type=radio] { display: none; } .switch label { display: block; float: left; padding: 3px 6px; -moz-border-radius: 5px; -webkit-border-radius: 5px; border-radius: 5px; color: #aaa; cursor: pointer; } .switch label:hover { text-shadow: 0 0 2px #fff; color: #888; } .switch label.checked { background: #8fc800; color: #eee; text-shadow: 0 -1px 1px rgba(0,0,0,0.5); background: -webkit-linear-gradient(top, #8fc800, #438c00); background: -moz-linear-gradient(top, #8fc800, #438c00); background: -ms-linear-gradient(top, #8fc800, #438c00); cursor: default; } </code></pre> <p>And html:</p> <pre><code>&lt;div class="switch"&gt; &lt;input type="radio" name="weekly_new" id="weekSW-0" &lt;?=$yes_checked?&gt; value="Yes" /&gt; &lt;label for="weekSW-0" id="yes"&gt;ON&lt;/label&gt; &lt;input type="radio" name="weekly_new" id="weekSW-1" &lt;?=$no_checked?&gt; value="No" /&gt; &lt;label for="weekSW-1" id="no"&gt;OFF&lt;/label&gt; &lt;/div&gt; </code></pre> <p>And jquery:</p> <pre><code>&lt;script&gt; $(".switch label:not(.checked)").click(function(){ var label = $(this); var input = $('#' + label.attr('for')); if (!input.prop('checked')){ label.closest('.switch').find("label").removeClass('checked'); label.addClass('checked'); input.prop('checked', true); } }); $(".switch input[checked=checked]").each(function(){ $("label[for=" + $(this).attr('id') + "]").addClass('checked'); }); &lt;/script&gt; </code></pre> <p>As you can see the label.checked gives the button it's 'look' - what I want to do is have a different color for when No is selected as opposed to yes - red for no, green for yes, groundbreaking huh?</p> <p>Anyway, cannot for the life of me figure out if it can be done</p> <p>I assigned IDs to the labels and tried e.g.</p> <pre><code>.switch label.checked #yes { blah blah } </code></pre> <p>But this rendered the style useless!</p> <p>As I've done the code this way, can this be done?</p> <p>I've created a fiddle if easier to fiddle with... obv had to put valuse checked=checked as I normally load from mysql table but it cannot</p> <p><a href="http://jsfiddle.net/aS69U/" rel="nofollow">http://jsfiddle.net/aS69U/</a></p>
    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.
 

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