lundi 19 juin 2017

How to target only checkboxes that are checked

I'm creating clones of checked input labels and outputting them in a header region. This is working as expected up until the form is submitted (it's using AJAX). I want to search for any inputs that were checked when the form was submitted and then recreate their clones.

The issue with the below code is that it is creating a clone for every input (all of which use the same class) irrespective of whether the input is checked or not. I assume that this is because the stuff inside the if is targeting all elements rather than just ones that are checked. When I use $(this) inside the if it doesn't appear to refer to only the elements that are checked and is instead an object.

How can I apply the stuff in the if statement to only those checkboxes that are checked?

if ($('.my-input').is(':checked')) {
  $('.my-input').next('label').clone().appendTo('.my-header');
  $('.my-input').next('label').addClass('is-checked');
}




Aucun commentaire:

Enregistrer un commentaire