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

131
Views
¿Cómo ocultar un elemento si la entrada está vacía?

Soy un estudiante que aprende en javascript y estoy tratando de crear una función de búsqueda.
Tengo múltiples figuras html con imágenes. Estoy filtrando las figuras con una entrada y con los figcaptions.
Estoy tratando de ocultar las cifras que no coinciden con el valor que se ingresa en la entrada.
Pero tengo una figura que ya tiene una display:none; .
Y cuando busca esta figura determinada, aparece, pero después de que la entrada está vacía nuevamente, la figura permanece visible.
Traté de restablecer el css usando obj.style.display = "" y obj.style.display = "none" . Pero eso no funcionó.
Brevemente, mi objetivo es ocultar una figura hasta que coincida con el valor de la entrada, pero si la entrada está vacía, debe ocultarse nuevamente.
Cualquier ayuda sería apreciada, ¡gracias!

 // entire code can be found here: https://codepen.io/Yung_n-d/pen/mdwQGqX //Javascript function myFunction() { var input, filter, ul, li, a, i, txtValue, getSpecial; input = document.getElementById("myInput"); filter = input.value.toUpperCase(); ul = document.getElementById("background"); li = ul.getElementsByTagName("figure"); getSpecial = document.getElementsByClassName('special'); for (i = 0; i < li.length; i++) { a = li[i].getElementsByTagName("figcaption")[0]; txtValue = a.textContent || a.innerText; if (txtValue.toUpperCase().indexOf(filter) > -1 ) { li[i].style.display = ""; } else { li[i].style.display = "none"; } if(filter == 0){ getSpecial.style.display = ""; } } } //HTML <input type="text" id="myInput" onkeyup="myFunction()" placeholder="Zoek workshops.." title="Type in a name"> <div id="background"> <div id="gallery"> <figure class="pic1 4-5 6-8 8-12 12+ Dans"> <a href="workshopPages/dans/index.php"> <img src="media/fotos/gallery/Dans.png" /> <figcaption class="test">Dans</figcaption> </figure> <figure class="special" style="display: none;" id="8-12" class="pic10 4-5 6-8 8-12 12+ ckv"> <a href="workshopPages/reeks/index.php"> <img src="media/fotos/gallery/reeks.png" /> <figcaption>Film</figcaption> </figure> </div> </div>
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

'SomeString'.indexOf('') devolverá 0 en lugar de -1; También debe probar que el campo de valor de la entrada no esté en blanco.

Cambie la siguiente línea de:

 if (txtValue.toUpperCase().indexOf(filter) > -1 ) {

A:

 if (filter && txtValue.toUpperCase().indexOf(filter) > -1 ) {
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!