Toggle a switch from active(clicked) to inactive (not clicked) with JavaScript

Toggle a switch from active(clicked) to inactive (not clicked) with JavaScript




  $("#btnclear").click(function (e) {
            
       var special = document.querySelector('#Input-button-id');
        if (localStorage.backgroundModeOption == 1) {
            $(special).attr("checked", true);
        } else {
            $(special).attr("checked", false);
        }

        if (typeof Event === 'function' || !document.fireEvent) {
            var event = document.createEvent('HTMLEvents');
            event.initEvent('change', true, true);
            special.dispatchEvent(event);
        } else {
            special.fireEvent('onchange');
        }    
   });

This code works perfectly..

Comments

Popular posts from this blog

Validate Mobile Number with 10 Digits in ASP.Net