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

100
Views
Evento OnClick para ocultar y alternar la sección de la página

Creé dos (2) secciones. Quiero ocultar una sección de forma predeterminada, con un botón debajo que dice "Mostrar más".

Algo como esto --- ( https://www.livermoretreeremoval.com/tree-service-ulmar ) en la sección " Ulmar Tree Services Top Sights ".

Si se hace clic en el botón, debería aparecer la sección oculta y el texto del botón debería cambiar a "Mostrar menos".

Si se vuelve a hacer clic en el botón, la sección visible debería volver a ocultarse y el texto del botón debería decir "Mostrar más".

Entonces, cuando está oculto, el botón dice "Mostrar más", y cuando está visible, el botón dice "Mostrar menos".

Aquí está mi JavaScript a continuación:

`

 function toggleBlock() { // get the block var myBlock = document.getElementById('service-area-two'); // get the current value of the block's display property var displaySetting = myBlock.style.display; // also get the block button, so we can change what it says var blockButton = document.getElementById('Show More'); // now toggle the block and the button text, depending on current state if (displaySetting == 'block') { // block is visible. hide it myBlock.style.display = 'none'; // change button text blockButton.innerHTML = 'Show Less'; } else { // block is hidden. show it myBlock.style.display = 'block'; // change button text blockButton.innerHTML = 'Show More'; } }

`

El ID de la sección: service-area-two El nombre del botón es: ToggleButton

Sé que estoy haciendo muchas cosas mal.

¿Cómo puedo hacer que este botón funcione?

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

0

Usa el atributo oculto.
Asi que:

 <div hidden id="service-area-two"> // content </div> <button id="Show More" onclick="toggleBlock()">Show more</button>

Y la lógica de JavaScript así:

 function toggleBlock() { var myBlock = document.getElementById("service-area-two"); var blockButton = document.getElementById("Show More"); if (myBlock.hidden) { blockButton.innerHTML = "Show Less"; myBlock.hidden = false; } else { blockButton.innerHTML = "Show More"; myBlock.hidden = true; } }
about 4 years ago · Juan Pablo Isaza Report

0

Entonces, en cuanto al botón, mostrar más y mostrar menos no funciona, es lo que entendí. Creo que el error aquí es innerHTML . innerHTML significa el HTML real dentro del bloque como por ejemplo:

button.innerHTML = '<img src="---'">'

Finalmente, cambia innerHTML a innerText .

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!