mercredi 14 août 2019

How do I check if a checkbox is checked?

I would like to append some text to a <span> after checking whether or not a checkbox has been checked. I have multiple checkboxes on the page and I only want to append the <span> once. All the solutions I have seen don't seem to work for me. I have tried to check by:

if ($('input:checkbox[name="skills"]').prop('checked')) {
   $('#filter-7').append($('#headingSkills a').text());
}

And:

if ($('input:checkbox[name="skills"]').is(':checked')) {
    $('#filter-7').append($('#headingSkills a').text());
}

Neither seem to work and I don't want to add the append() to my change function because it appends the span multiple times and I only want it to be appended when the checkbox is checked once.

Here is my change function:

$('input:checkbox[name="skills"]').change(function () {
    var values = $('input:checkbox[name="skills"]:checked').map(function () {
      return '<span><button class="remove-selection" value="' + this.value.trim() + '"></button> ' + this.value.trim() + '</span>';
    }).get();

    $('#filter-7').append($('#headingSkills a').text());

    $('#filter-7-result').empty().append(values);
});

I know this a commonly asked question but nothing I have tried seems to work.




Aucun commentaire:

Enregistrer un commentaire