Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

102
Vistas
OnClick event to Hide and Toggle Page Section

I created two(2) sections.I want to hide a section by default, with a button under it that says “Show More”.

Something like this--- (https://www.livermoretreeremoval.com/tree-service-ulmar) under the "Ulmar Tree Services Top Sights" section.

If the button is clicked, the hidden Section should appear, and the button Text should change to “Show Less”.

If the Button is clicked again, the visible Section should become hidden again, and the Button Text should say “Show More”.

So when hidden, the button says “Show More”, and when visible the button says “Show Less”.

Here’s my JavaScript below:

`

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’;
}
}

`

The Section ID: service-area-two The Button Name is: ToggleButton

I know I’m doing a lot of things wrong.

How can I get this button to work?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Use the hidden attribute.
So:

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

And the JavaScript logic like this:

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 Denunciar

0

So as for the button, the show more and show less is not working is what I understood. I think the mistake here is innerHTML. innerHTML means the actual HTML inside the block like for example:

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

Finally, change innerHTML to innerText.

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda