mercredi 25 août 2021

Change checkbox value on click

I have checkbox nested in html elements. I want to target it and manipulates its value when somebody check it. I am trying this

jQuery(document).ready(function() {
  jQuery('#my-form .checkbox-value input').click(function() {
    jQuery('#my-form .checkbox-value input[value=yes]').prop('checked', jQuery(this).prop('checked'));
    jQuery('#my-form .checkbox-value input[value=no]').prop('checked', jQuery(this).prop('checked') === false);
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form id="my-form">
  <div id="checkbox" class="checkbox-value">
    <label class="form-label forms-label-hide" for="checkbox_item">Checkboxes</label>
    <ul id="checkbox_item">
      <li class="choice-1 depth-1">
        <input type="checkbox" id="checkbox_item_1" name="my-checkbox" value="Want to change this value on dom" />
        <label for="checkbox_item_1">Yes, let me know when you have new blogs to share.</label>
      </li>
    </ul>
  </div>

  <div class="">
    <button type="submit">Submit</button>
  </div>

</form>

When someone click on label or checkbox it should changes values to yes if it is checked and no when it is not checked.




Aucun commentaire:

Enregistrer un commentaire