0
votes

I need to select all and deselect all check boxes on a button toggle.

here is the html code for check-boxes

<div class="accordion-header js-accordion-header">
<input type="checkbox" class="to-labelauty-icon labelauty" name="inputLableautyNoLabeledCheckbox" data-plugin="labelauty" data-label="false" id="labelauty-2" value="tc_Logout" aria-hidden="true" style="display: none;">
<label for="labelauty-2">
<span class="labelauty-unchecked-image"></span>
<span class="labelauty-checked-image"></span>
</label>  tc_Logout
</div>

when I select this check box aria-checked="true" is automatically added to <label for="labelauty-2"> which output this

<label for="labelauty-1" aria-checked="true">
<span class="labelauty-unchecked-image"></span>
<span class="labelauty-checked-image"></span>
</label>

It changes the status to true or false when its checked and unchecked.

Now I have several check-boxes like this which needs to be checked or unchecked using a toggle button.

here is my html code for toggle button

<div class="toggle-wrap w-checkbox float-right">
  <input class="toggle-ticker w-checkbox-input" data-ix="toggle-switch" data-name="Toggle Switch" id="Toggle-Switch" name="Toggle-Switch" type="checkboxtoggle" onclick="toggle()">
  <label class="toggle-label w-form-label" for="Toggle-Switch"></label>
  <div class="toggle">
    <div class="toggle-active">
      <div class="active-overlay"></div>
      <div class="top-line"></div>
      <div class="bottom-line"></div>
    </div>
  </div>
</div>

Here is my JS code which returns an error "prop is not defined"

function toggle(source) {
  checkboxes = document.getElementsByName('inputLableautyNoLabeledCheckbox');
  for (var i = 0, n = checkboxes.length; i < n; i++) {
    checkboxes[i].checked = source.checked;
  }
}

How can I achieve this using javascript or jquery

2
you have to create a working example of your problem so that we can check and solve it. - Anant Kumar Singh

2 Answers

1
votes

Working ( without event listeners)

Note: See next example, that used event listeners and is preferred.

You had some issues I had to fix in order to get this working.

See: https://jsfiddle.net/0p64mdsg/4

First off, your input types were not actually toggles. Secondly, you were not passing this when you were calling the toggle function.

Current version of code in my jsFiddle:

function toggle(source) {
  checkboxes = document.getElementsByName('inputLableautyNoLabeledCheckbox');
  alert('TEST2');
  for (var i = 0, n = checkboxes.length; i < n; i++) {
    checkboxes[i].checked = source.checked;
  }
}

    
<div class="accordion-header js-accordion-header">
  <input type="checkbox" class="to-labelauty-icon labelauty" name="inputLableautyNoLabeledCheckbox" data-plugin="labelauty" data-label="false" id="labelauty-2" value="tc_Logout" aria-hidden="true" >
  <label for="labelauty-2">
  <span class="labelauty-unchecked-image"></span>
  <span class="labelauty-checked-image"></span>
  </label>  tc_Logout
  </div>

  <label for="labelauty-1" aria-checked="true">
  <span class="labelauty-unchecked-image"></span>
  <span class="labelauty-checked-image"></span>
  </label>
  Test1
  <div class="toggle-wrap w-checkbox float-right">
    <input class="toggle-ticker w-checkbox-input" data-ix="toggle-switch" data-name="Toggle Switch" id="Toggle-Switch" name="Toggle-Switch" type="checkbox" onclick="toggle(this)">
    <label class="toggle-label w-form-label" for="Toggle-Switch"></label>
    <div class="toggle">
      <div class="toggle-active">
        <div class="active-overlay"></div>
        <div class="top-line"></div>
        <div class="bottom-line"></div>
      </div>
    </div>
  </div>

Using Event Listeners

Since onClick is not preferred, you should handle the toggle click handling like this: https://jsfiddle.net/0p64mdsg/15/

<script>

document.getElementById("Toggle-Switch").addEventListener("click", toggle);

function toggle(source) {
  checkboxes = document.getElementsByName('inputLableautyNoLabeledCheckbox');
  alert('TEST2' + source);
  for (var i = 0, n = checkboxes.length; i < n; i++) {
    checkboxes[i].checked = document.getElementById("Toggle-Switch").checked;
  }
}

</script>
0
votes

Try This To select/Deselect All Chechbox

  $(document).on('click','#chkAll',function(){
  if($(this).prop('checked')==true){
  	$('.ChkSelect').attr("checked",true)
  }
  else
  {
  $('.ChkSelect').attr("checked",false)
  }  
   });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="checkbox" class="ChkSelect">
<input type="checkbox" class="ChkSelect">
<input type="checkbox" class="ChkSelect">
<input type="checkbox" class="ChkSelect">
<input type="checkbox" class="ChkSelect">
<input type="checkbox" class="ChkSelect">
<input type="checkbox" class="ChkSelect"><br/><br/>
Select/Deselect All :<input type="checkbox" id="chkAll"> <br/><br/>