CSS Toggle Switch with <select>

Asked

Viewed 466 times

2

I need to transition from this script to use with <select>.

See well the script working is like this http://callmenick.com/tutorial-demos/css-toggle-switch/

My selects (over 88) are ONLY in this style:

<select>
<option value="0">Off</option>
<option value="1" selected="selected">On</option>
</select>

Only with on and off option (0.1)

I wish I could adapt the CSS for this, but I see that it is CSS3 and I don’t know how to work with it.. What can I do?

Example of what it would be like http://jsfiddle.net/thebestclassdsfgf/jo2hkom2/1/

1 answer

3


This code is made for a input type="checkbox" and CSS does all the work. That is if the input is :checked or not, the button moves. In your case could work that way too, input.

But if you really want to use select can do so using a class ligado to replace the functionality of :checked

HTML

<select id="cmn-toggle-1" class="cmn-toggle cmn-toggle-round" >
    <option value="0">Off</option>
    <option value="1" selected="selected">On</option>
</select>
<label for="cmn-toggle-1"></label>

jQuery

$('select').on('click', function () {
     $(this).val(this.value == '1' ? '0' : '1');
    this.classList.toggle('ligado');
});

CSS (only the relevant part)

.cmn-toggle-round.ligado + label:before {
    background-color: #8ce196;
}
.cmn-toggle-round.ligado + label:after {
    margin-left: 60px;
}

jsFiddle: http://jsfiddle.net/y8u1beek/

  • perfect! But I would have problems, because I already define who is marked or not using the selected="selected" and in that case it didn’t work..

  • @user3163662 ok, that’s simple, you only need to add one more line: http://jsfiddle.net/7m8L2b74/2/

  • Okay, now I’m having problems, it doesn’t change the value.. As I’m going to post, I need you to change. See (change toggle and wait) jsfiddle.net/thebestclassdsfgf/7m8L2b74/3 @Sergio

  • @user3163662 and why not do this with checkbox?

  • @Segio I am making transitions.. and are many checkboxes

  • @user3163662 I edited the response to change the value of select when it receives a click

  • thank you very much, with your help I got =)

  • @user3163662 good!

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.