Skip to content

Instantly share code, notes, and snippets.

@kcmr
Created September 30, 2014 00:07
Show Gist options
  • Select an option

  • Save kcmr/399fa8f9db42ab12a2c1 to your computer and use it in GitHub Desktop.

Select an option

Save kcmr/399fa8f9db42ab12a2c1 to your computer and use it in GitHub Desktop.
A Pen by kcmr.

Falso select y toggle con input:checked

Sólo se usa javascript para cambiar el texto del select al seleccionar una opción.

A Pen by kcmr on CodePen.

License.

<div class="item">
<div class="select">
<input type="checkbox" id="show-options">
<label id="label" for="show-options">Seleccionar opción…</label>
<ul class="options">
<li>
<input type="radio" id="opt-1" name="opt">
<label for="opt-1">Opción 1</label>
</li>
<li>
<input type="radio" id="opt-2" name="opt">
<label for="opt-2">Opción 2</label>
</li>
<li>
<input type="radio" id="opt-3" name="opt">
<label for="opt-3">Opción 3</label>
</li>
<li>
<input type="radio" id="opt-4" name="opt">
<label for="opt-4">Opción 4</label>
</li>
<li>
<input type="radio" id="opt-5" name="opt">
<label for="opt-5">Opción 5</label>
</li>
</ul>
</div>
</div>
<div class="item">
<div class="toggle">
<div class="option on">
<input type="radio" id="on" name="on-off" checked>
<label for="on" class="on">ON</label>
</div>
<div class="option off">
<input type="radio" id="off" name="on-off">
<label for="off" class="off">OFF</label>
</div>
</div>
</div>
var radios = document.querySelectorAll('.select input[type="radio"]'),
i = 0,
len = radios.length,
label = document.querySelector('#label'),
select = document.querySelector('div.select'),
initialWidth = select.clientWidth;
select.style.cssText = 'width: ' + initialWidth + 'px';
for (; i < len; i++) {
radios[i].addEventListener('click', function(){
label.innerHTML = document.querySelector('label[for="'+this.getAttribute('id')+'"]').innerHTML;
document.querySelector('#show-options').click();
});
}
body {
font-family: arial, sans-serif;
}
.item {
float: left;
margin-right: 20px;
min-width: 200px;
}
/**
* select
*/
.select {
border-radius: 8px;
background-image: linear-gradient(-179deg, #FEFDFD 0%, #E9E9E9 100%);
border: 1px solid #CBCACA;
display: inline-block;
overflow: hidden;
}
.select label {
font-weight: bold;
font-size: 14px;
cursor: pointer;
padding: 8px 40px 8px 10px;
display: block;
border-radius: 8px;
position: relative;
white-space: nowrap;
}
.select > label:after {
content: "";
width: 8px; height: 8px;
border: 1px solid #5a5a5a;
border-width: 0 2px 2px 0;
position: absolute;
right: 12px; top: 9px;
transform: rotate(45deg);
transition: all .2s;
}
.select input[type="checkbox"],
.select input[type="radio"] {
position: absolute !important;
clip: rect(1px 1px 1px 1px);
clip: rect(1px, 1px, 1px, 1px);
}
.select input[type="checkbox"]:checked ~ .options {
max-height: 2000px;
}
.select input[type="checkbox"]:checked ~ label:after {
transform: rotate(225deg);
top: 15px;
}
.select input[type="radio"]:checked ~ label {
color: #858585;
}
.select .options {
list-style: none;
margin: 0;
padding: 0;
max-height: 0;
transition: max-height .2s;
}
/**
* TOGGLE
*/
.toggle input[type="radio"] {
position: absolute !important;
clip: rect(1px 1px 1px 1px);
clip: rect(1px, 1px, 1px, 1px);
}
.toggle label {
float: left;
font-size: 14px;
padding: 8px 12px 9px;
font-weight: bold;
cursor: pointer;
line-height: 16px;
}
.toggle input[type="radio"]:checked ~ label {
cursor: default;
}
.toggle .on label {
border-radius: 8px 0 0 8px;
background-image: linear-gradient(-179deg, #D8D8D8 0%, #E2E0E0 23%, #EBE9E9 78%, #D8D8D8 100%);
box-shadow: inset 0 -3px 0 rgba(0,0,0,.22);
color: #999898;
}
.toggle .off label {
border-radius: 0 8px 8px 0;
background-image: linear-gradient(-179deg, #D8D8D8 0%, #E2E0E0 23%, #EBE9E9 78%, #D8D8D8 100%);
box-shadow: inset 0 -3px 0 rgba(0,0,0,.22);
color: #999898;
}
.toggle .on input[type="radio"]:checked ~ label {
border-radius: 8px 2px 2px 8px;
background-image: linear-gradient(-179deg, #9BCB44 0%, #429321 100%);
box-shadow: 1px 1px 1px 0px rgba(139,138,138,0.36), inset 0px -6px 0px 0px rgba(0,0,0,0.18);
margin-right: -2px;
position: relative;
padding: 8px 12px 11px;
top: -2px;
color: #fff;
text-shadow: 0px -1px 0px rgba(0,0,0,0.50);
}
.toggle .off input[type="radio"]:checked ~ label {
border-radius: 2px 8px 8px 2px;
background-image: linear-gradient(-179deg, #F5515F 0%, #9F041B 100%);
box-shadow: -1px 1px 1px 0px rgba(139,138,138,0.36), inset 0 -6px 0 rgba(0,0,0, .22);
margin-left: -2px;
position: relative;
padding: 8px 12px 11px;
top: -2px;
color: #fff;
text-shadow: 0px -1px 0px rgba(0,0,0,0.50);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment