I have been looking around for a 3 state toggle switch but haven't had much luck.
Basically I need a switch that has the states: | ON | N/A | OFF |
The slider by default starts in the middle, and once the user slides to left or right, they can't go back to the N/A (not answered) state.
Anyone have any idea on how to handle this?
The toggle() method toggles between hide() and show() for the selected elements. This method checks the selected elements for visibility. show() is run if an element is hidden. hide() is run if an element is visible - This creates a toggle effect.
Definition. Toggle Switches have an operating lever that can be pushed up and down or left and right to switch an electrical circuit. A “toggle” is a small wooden rod that is used as a clothing fastener in the place of buttons.
Try something like this:
.switch-toggle {
width: 10em;
}
.switch-toggle label:not(.disabled) {
cursor: pointer;
}
<link href="https://cdn.jsdelivr.net/css-toggle-switch/latest/toggle-switch.css" rel="stylesheet" />
<div class="switch-toggle switch-3 switch-candy">
<input id="on" name="state-d" type="radio" checked="" />
<label for="on" onclick="">ON</label>
<input id="na" name="state-d" type="radio" disabled checked="checked" />
<label for="na" class="disabled" onclick=""> </label>
<input id="off" name="state-d" type="radio" />
<label for="off" onclick="">OFF</label>
<a></a>
</div>
This will start with N/A
as the default option (via checked="checked"
), but make it unselectable later (by using disabled
)
JSFiddle Demo (Simplified)
.switch-toggle {
float: left;
background: #242729;
}
.switch-toggle input {
position: absolute;
opacity: 0;
}
.switch-toggle input + label {
padding: 7px;
float:left;
color: #fff;
cursor: pointer;
}
.switch-toggle input:checked + label {
background: green;
}
<div class="switch-toggle switch-3 switch-candy">
<input id="on" name="state-d" type="radio" checked="" />
<label for="on" onclick="">ON</label>
<input id="na" name="state-d" type="radio" checked="checked" />
<label for="na" class="disabled" onclick="">N/A</label>
<input id="off" name="state-d" type="radio" />
<label for="off" onclick="">OFF</label>
</div>
As a jQuery plugin
function filterme(value) {
value = parseInt(value, 10); // Convert to an integer
if (value === 1) {
$('#RangeFilter').removeClass('rangeAll', 'rangePassive').addClass('rangeActive');
$('span').text('Active');
} else if (value === 2) {
$('#RangeFilter').removeClass('rangeActive', 'rangePassive').addClass('rangeAll');
$('span').text('All');
} else if (value === 3) {
$('#RangeFilter').removeClass('rangeAll', 'rangeActive').addClass('rangePassive');
$('span').text('Passive');
}
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="range-field" style=" width:60px">
<input type="range" id="RangeFilter" name="points" onchange="filterme(this.value);" min="1" class="rangeAll" max="3" value="2">
</p>
<span>All</span>
(function($) {
$.fn.removeClasses = function(classes) {
return this.removeClass(classes.join(' '));
};
$.fn.switchify = function(config) {
config = config || {};
var prefix = config.prefix || 'range-';
var onCls = prefix + (config.onCls || 'on' );
var offCls = prefix + (config.offCls || 'off' );
var unsetCls = prefix + (config.unsetCls || 'unset');
var $self = this;
return this.on('change', function(e) {
var value = parseInt(this.value, 10);
switch (value) {
case 1 : return $self.removeClasses([unsetCls, offCls]).addClass(onCls);
case 2 : return $self.removeClasses([onCls, offCls]).addClass(unsetCls);
case 3 : return $self.removeClasses([onCls, unsetCls]).addClass(offCls);
default : return $self;
}
});
};
})(jQuery);
$('#range-filter').switchify({
onCls : 'active',
offCls : 'passive',
unsetCls : 'all'
}).on('change', function(e) {
var $self = $(this);
if ($self.hasClass('range-active')) $('span').text('Active');
else if ($self.hasClass('range-passive')) $('span').text('Passive');
else if ($self.hasClass('range-all')) $('span').text('All');
else $('span').text('Error!');
});
.range-field { width: 60px; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p class="range-field">
<input type="range" id="range-filter" name="points" min="1" class="rangeAll" max="3" value="2">
</p>
<span>All</span>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With