I've created a 3 way toggle with 3 states disabled, default, enabled.
On clicking each input, the corresponding div's should be displayed.
var content = function() {
var divs = ["div-data1", "div-data2", "div-data3"];
var visibleDivId = null;
function toggle() {
//code
}
function init() {
toggle();
}
return {
init: init,
}
}();
window.onload = function() {
content.init();
};
<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.2.1/css/all.min.css" rel="stylesheet"/>
<div class="tw-toggle">
<input type="radio" name="toggle" class="threeToggle1" value="false">
<label class="toggle toggle-yes"><i class="fa fa-times"></i></label>
<input checked type="radio" name="toggle" class="threeToggle2" value="-1">
<label class="toggle toggle-yes"><i class="fa fa-minus"></i></label>
<input type="radio" name="toggle" class="threeToggle3" value="true">
<label class="toggle toggle-yes"><i class="fa fa-check"></i></label>
</div>
<div class="div-data1" style="display:none"> div1 </div>
<div class="div-data2" style="display:none"> div2</div>
<div class="div-data3" style="display:none">div3 </div>
How could the toggle works, without passing onclick to HTML ?
Could someone please help.
Thanks.
One approach is as follows, with explanatory comments in the code:
References:
Array.prototype.filter().Array.prototype.findIndex().Array.prototype.forEach().Element.children.Event()constructor.EventTarget.addEventListener().EventTarget.dispatchEvent().HTMLElement.style.