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

261
Vistas
How do I make a button show and hide text when clicked and why does getElement not work?

I have done all the HTML and CSS but the JavaScript part is confusing me. How do I make a button hide or show text when clicked?

There is an error that comes up on the console as well. It says that getElement is not defined. I've also tried using getElementByClassName but the error message still comes up.

The error says Uncaught ReferenceError: getElement is not defined and the same for getElementByClassName

--

In the code:

nav-btn = the button that should to toggle the text

nav-links = the text that should to show up

--

The JavaScript code:

const links = getElement(".nav-links")
const navBtn = getElementByClassName('nav-btn')

links.style.display = 'none';

navBtn.onclick = function () {
    if (links.style.display !== "none") {
        links.style.display = "none"
    } else {
        links.style.display = "block"
    }
}

What do I need to change to make it work?

Also what do I do about the getElement problem?

about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Since getElement is not defined, you must be using it as native JS function, which is not available.
If you want to select element with className, you can use querySelector

const links = document.querySelector('.nav-links')

And getElementsByClassName returns an array of nodes, not just single element. So Change it to querySelector for your use case

const navBtn = document.querySelector('.nav-btn')
about 4 years ago · Santiago Trujillo Denunciar

0

As mentioned in the comment below your post getElement isn't a native JS DOM method which is why the error is appearing. Personally I would use querySelector, and add a listener to the button. And then use CSS, and classList, to toggle a show class off and on. It makes for shorter more readable code.

const links = document.querySelector(".nav-links")
const navBtn = document.querySelector('.nav-btn')

navBtn.addEventListener('click', handleClick, false);

function handleClick() {
  links.classList.toggle('show');
}
.nav-links { display: none; }
.show { display: block; }
<div class="nav-links">Nav links</div>
<button class="nav-btn">Toggle the nav!</button>

about 4 years ago · Santiago Trujillo 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