Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

391
Visualizações
Js: Function that Hides Divs According to an array?

I have divs ('items') with checkboxes inside each. How can I hide the items that doesn't have the checkboxes with the selected values checked?

<div class='item'> <input type='checkbox' value='green' checked></input> <input type='checkbox' value='red'></input> </div>
<div class='item'> <input type='checkbox' value='yellow'></input> <input type='checkbox' value='orange'></input> </div>


function MyFunction () {

const values = ['green', 'red'];
let items = document.querySelectorAll('.item');
items.forEach(items => {
    if (values.every(values => 

        items.querySelectorAll('input[type="checkbox"]:checked')[0].value.includes(values) // working (only for the first checked checkbox)

        items.querySelectorAll('input[type="checkbox"]:checked').forEach((cb, i) => // not working
              cb[i].value.includes(values)
        )
        
    ))
    {
        items.style.display = 'block'
    }
    else {
        items.style.display = 'none'
    }
})

}

In the example above, only the first item should be shown after firing myFunction(), because it has the value green from values, and it is checked.

enter image description here

Thanks!

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

<html>
<body>
    <div class='item'>
        <input type='checkbox' value='green' checked>Green</input>
        <input type='checkbox' value='red'>Red</input>
    </div>
    <div class='item'>
        <input type='checkbox' value='yellow'>Yellow</input>
        <input type='checkbox' value='orange'>Orange</input>
    </div>

<script>
    function MyFunction () {
        const values = ['green', 'red'];
        let items = document.querySelectorAll('.item');
        items.forEach(item => {
            if([].slice.call(item.children).filter(a => a.checked).length > 0) {
                item.style.display = 'block';
            } else {
                item.style.display = 'none';
            }
        });
    }
    MyFunction();
</script>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda