samedi 11 novembre 2017

Tell whether checkbox is checked from HTMLInputElement in javascript

I have a list of checkboxes in HTML, and I want to get the values for only the checked checkboxes. HTML input element being referenced in javascript like so:

function getRandomGenre() {
    const checks = document.getElementById("checks")
    const genre_checkboxes = Array.from(checks.getElementsByTagName("input"))
    console.log(genre_checkboxes)
    const checked_genres = genre_checkboxes.filter((c) => { c.checked }) // fails here
    if (checked_genres.length == 0) {
        return "NA"
    } else {
        return checked_genres[Math.floor(Math.random() * checked_genres.length)].value
    }
}

But I noticed that the HTMLInputElement Object doesn't have a "checked" property. How do I get this property? Below I've incluided a screenshot of the top few properties I see. "checked" is not among them.

HTMLInputElement




Aucun commentaire:

Enregistrer un commentaire