It appears that there is an issue with the functionality when switching values on page load.
Initially, I was able to make it work for a single switch, but now that there are multiple switches on the page, toggling affects all of them. How can I modify it to work independently for each one?
$('.switch .inactive').hide();
var switchStatus = $('.switch').next('input').val();
// 1 = disable
// 0 = enabled
if (switchStatus == '1') {
$('.switch .active').hide();
} else {
$('.switch .active').show();
}
$('.switch').click(function() {
$('.inactive, .active').toggle();
var featureStatus = $(this).find('#ProductFeatures_TP_Status').val();
//console.log(featureStatus);
if (featureStatus == "1") {
$(this).find('#ProductFeatures_TP_Status').val('0');
} else {
$(this).find('#ProductFeatures_TP_Status').val('1');
}
});
.panel-heading {
cursor: pointer;
margin: 0;
}
.panel-heading .status {
float: right;
}
.panel-collapse {
display: none;
}
.panel-collapse.open {
display: block;
}
.panel-body .col-md-6.left {
padding-left: 0;
}
.panel-body .col-md-6.right {
padding-right: 0;
}
.panel-body .redactor-editor {
min-height: 100px !important;
}
.switch {
float: left;
font-size: 1.5em;
margin: -4px 10px 0 -5px;
}
.switch .active {
color: green;
}
<link href="https://maxcdn.bootstrapcdn.com/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js"></script>
<div class="switch ProductFeatures_TP_Status">
<i class="fa fa-toggle-on active"></i>
<i class="fa fa-toggle-on fa-rotate-180 inactive"></i>
<input type="hidden" id="ProductFeatures_TP_Status" name="ProductFeatures_TP_Status" value="0">
</div>
<div class="switch ProductFeatures_TP_Status">
<i class="fa fa-toggle-on active"></i>
<i class="fa fa-toggle-on fa-rotate-180 inactive"></i>
<input type="hidden" id="ProductFeatures_TP_Status1" name="ProductFeatures_TP_Status1" value="1">
</div>
<div class="switch ProductFeatures_TP_Status">
<i class="fa fa-toggle-on active"></i>
<i class="fa fa-toggle-on fa-rotate-180 inactive"></i>
<input type="hidden" id="ProductFeatures_TP_Status2" name="ProductFeatures_TP_Status2" value="1">
</div>