I am looking to modify the appearance of an HTML/CSS/jQuery toggle element based on the value of a cell within a table, either displaying "YES" or "NO".
The required functionality includes:
If the text in the table cell is "yes", the toggle element should have a class of
.off
and set the checked attribute of the checkbox totrue
.If the cell contains the text "no", the toggle element should have a class of
.on
and set the checked attribute of the checkbox tofalse
.
Below is the code I have written so far:
// TRIM FUNCTION
if (typeof String.prototype.trim !== 'function') {
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g, '');
}
}
// TOGGLE FUNCTIONALITY
$(document).ready(function() {
// FIND DEV YES/NO INPUT & CHECK VALUE
var ynCell = $("td.yn");
$(ynCell).each(function() {
var ynValue = $(ynCell).text().toLowerCase().trim();
// IF VALUE = NO, DISPLAY TOGGLE CLASS 'ON'
// IF VALUE = YES, DISPLAY TOGGLE CLASS 'OFF'
if (ynValue.indexOf('no') != -1) {
$(".switch").parent().find('input:checkbox').attr('checked', false);
$(".switch").removeClass('off').addClass('on');
} else if (ynValue.indexOf('yes') != -1) {
$(".switch").parent().find('input:checkbox').attr('checked', true);
$(".switch").removeClass('on').addClass('off');
}
});
$(".switch").each(function() {
if ($(this).parent().find('input:checkbox').length) {
if (!$(this).parent().find('input:checkbox').hasClass("show")) {
$(this).parent().find('input:checkbox').hide();
}
if ($(this).parent().find('input:checkbox').is(':checked')) {
$(this).removeClass('on').addClass('off');
} else {
$(this).removeClass('off').addClass('on');
}
}
});
$(".switch").click(function() {
if ($(this).hasClass('on')) {
$(this).parent().find('input:checkbox').attr('checked', true);
$(this).removeClass('on').addClass('off');
} else {
$(this).parent().find('input:checkbox').attr('checked', false);
$(this).removeClass('off').addClass('on');
}
});
});
th {
text-align: left;
}
.switch-container {
padding: 5px;
}
.switch {
position: relative;
display: inline-block;
font-size: 1.6em;
font-weight: bold;
color: #ccc;
height: 18px;
padding: 6px 6px 5px 6px;
border: 1px solid #ccc;
border-radius: 5px;
background: #ececec;
cursor: pointer;
font-size: 14px;
}
body.IE7 .switch {
width: 78px;
}
.switch span {
display: inline-block;
width: 35px;
}
.switch span.on {
color: #5cbacc;
margin-left: 5px;
}
.switch span.off {
margin-left: 10px;
}
.switch .toggle {
position: absolute;
top: 1px;
width: 40px;
height: 25px;
border: 1px solid #ccc;
border-radius: 5px;
background: #fff;
z-index: 999;
-webkit-transition: all 0.15s ease-in-out;
-moz-transition: all 0.15s ease-in-out;
-o-transition: all 0.15s ease-in-out;
-ms-transition: all 0.15s ease-in-out;
}
.switch.on .toggle {
left: 1%;
}
.switch.off .toggle {
left: 56%;
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<table style="width:100%">
<tr>
<th>Yes/No</th>
<th>Toggle</th>
</tr>
<tr>
<td class="yn">Yes</td>
<td class="switch-container">
<input type="checkbox" checked>
<div class="switch"><div class="toggle"></div>
<span class="on">YES</span><span class="off">NO</span></div></td>
</tr>
<tr>
<td class="yn">No</td>
<td class="switch-container">
<input type="checkbox" checked>
<div class="switch"><div class="toggle"></div>
<span class="on">YES</span><span class="off">NO</span></div></td>
</tr>
</table>
<script
src="https://code.jquery.com/jquery-3.2.1.min.js"
integrity="sha256-hwg4gsxgFZhOsEEamdOYGBf13FyQuiTwlAQgxVSNgt4="
crossorigin="anonymous"></script>
</body>
</html>
Any recommendations on enhancing my script and resolving any issues are appreciated.
Here is a link to a JS Bin for testing: https://jsbin.com/derevarixo/edit?html,css,js,output