mercredi 24 avril 2019

select only one checkbox in a group not working 100%

Hello I'm trying to make it where the user can only select 1 checkbox at a time on a page.

Here's my code so far:

<script>
function onlyOne(checkbox) {
    var checkboxes = document.getElementsByName('active', 'inactive', 
'showall')
    checkboxes.forEach((item) => {
        if (item !== checkbox) item.checked = false
    })
}
</script>

and

<strong>Active</strong>
<input type="checkbox" name="active" value="Yes" onclick="onlyOne(this)">
<strong>Inactive</strong>
<input type="checkbox" name="inactive" value="Yes" 
onclick="onlyOne(this)">
<strong>Show All</strong>
<input type="checkbox" name="showall" value="Yes" onclick="onlyOne(this)">

What keeps happening is it will work sometimes and sometimes they can select more than 1 checkbox. What do I need to tweak to get it working all the time.

Thanks




Aucun commentaire:

Enregistrer un commentaire