Search code examples
javascriptjqueryswitchery

Check uncheck switcher


Hello I am using abpetkov/switchery to render those beautiful switches. There is a problem that I am facing which is that I want to enable or disable switches using jquery and by enable and disable I mean check and uncheck. I can't figure out how to do it. Can somebody tell me how to do it I'm rendering it like this

<table>
    <tr>
      <td>
        <input type='checkbox' name='record-call' data-render='switchery' data-theme='default' id="abcd"  /></td>
      </td>
    </tr>   
</table>

the problem that I'm facing is whenever I try to check or uncheck the switcher like this $( "#hour-1" ).prop( "checked", true ); it checks/unchecks the check box but does not effects the switcher


Solution

  • Capture the onchange event and then trigger the click event. I kind of did it like this e.next.click() You must be wondering why I wrote next. Well if you inspect you input box you will see that there is a span underneath the input box. that is your switcher. just trigger the click event on that.

    <table>
        <tr>
          <td>
            <input type='checkbox' name='record-call' data-render='switchery' data-theme='default' id="abcd"  onchange="fun(this)"/></td>
          </td>
        </tr>   
    </table>
    
    
    function fun(e){
       e.next.click()
    }
    

    Hope it helps. Feel free to ask anything if you have any confusion.