Convert dynamically created checkbox input to Switchery-style switch

418 Views Asked by At

I need to dynamically create a Switchery switch.

I'm creating non-dynamic switches like this:

<div class="item form-group">
   <label class="control-label col-md-3 col-sm-3 col-xs-12">Admin User</label>
   <div class="col-md-6 col-sm-6 col-xs-12">
      <input type="checkbox" class="js-switch" id= "canModify"/>
   </div>
</div> 

And this works just fine.

However when I create the same structure via JavaScript like this:

  html = '<input type="checkbox" class="js-switch" id= "' + elementID +  '"/>'

I get these results:

Not loading the look of the js-switch

The one below was statically added, and it appears correctly. The one above generated dynamically, but it appears only as a check box. So my question is, how do I solve this?

2

There are 2 best solutions below

0
On

After looking at the documentation I did find somehting that worked.

I need to first add the code to the actual elment that will contain the switch.

   document.getElementById("mainPanel").innerHTML = html;

Only after the code is added I can enable switchery using these two lines, copied and pasted from the documentantion

   // This is required to enable Switchery. 
   var elem = document.querySelector('.js-switch');
   var init = new Switchery(elem);   

Hope this helps someone else!

7
On

Simply adding a new input to the DOM with class="js-switch" will not create a Switchery-style switch.

A JavaScript Switchery object must be created for each input.

This function will convert all non-switchery inputs in the DOM to Switchery-style switches:

// convert all inputs with class="js-switch" to Switchery
function convertToSwitchery() {

  // get all "js-switch" class inputs in the DOM
  const jsSwitchInputs = document.querySelectorAll('.js-switch');

  jsSwitchInputs.forEach(input => {
    // ignore if input is already switchery
    if (input.dataset.switchery) {
      return;
    }
    // create new Switchery object
    new Switchery(input);
  });
}

A working example:

// convert all inputs with class="js-switch" to Switchery-style
function convertToSwitchery() {
  const jsSwitchInputs = document.querySelectorAll('.js-switch');
  jsSwitchInputs.forEach(input => {
    // ignore if input is already switchery
    if (input.dataset.switchery) {
      return;
    }
    new Switchery(input);
  });
}



// code for demo only:
const divResult = document.getElementById('result');
let iButton = 0;

// add a new "js-switch" input
function addInput() {
  const br = document.createElement('br');
  divResult.appendChild(br);
  const label = document.createElement('label');
  label.for = `input${++iButton}`;
  label.className = 'mr-4';
  label.innerHTML = `Dynamic "js-switch" ${iButton} `;
  divResult.appendChild(label);
  const input = document.createElement('input');
  input.type = 'checkbox';
  input.className = 'js-switch';
  input.id = `input${iButton}`;
  divResult.appendChild(input);
}
<link href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" rel="stylesheet" />
<link href="https://cdn.jsdelivr.net/npm/[email protected]/switchery.css" rel="stylesheet" />

<div class="p-4">
  <h4>Convert Checkbox Inputs to Switchery Style</h4>
  <button class="btn btn-primary btn-sm" onclick="addInput();">
    Add ".js-switch"</button>
  <button class="btn btn-primary btn-sm"
    onclick="convertToSwitchery();">
    Convert All to Switchery</button>
    
  <div class="mt-4" id="result">
    <label for="static-input">Static "js-switch"</label>
    <input type="checkbox" class="js-switch" checked />
  </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/switchery.min.js"></script>

References: