Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

379
Views
Js: ¿Función que oculta Divs según una matriz?

Tengo divs ('elementos') con casillas de verificación dentro de cada uno. ¿Cómo puedo ocultar los elementos que no tienen las casillas de verificación con los valores seleccionados marcados?

 <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' } }) }

En el ejemplo anterior, solo se debe mostrar el primer elemento después de activar myFunction(), porque tiene el valor verde de valores y está marcado.

ingrese la descripción de la imagen aquí

¡Gracias!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!