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

251
Views
Activar contenido DIV al hacer clic en el botón + cambiar el texto del botón

Estoy tratando de activar la visibilidad de un DIV a través de un botón.

Mi código se ve así:

 function myFunction() { var moreText = document.getElementById("csrmore"); var x = document.getElementById("myDIV"); let ishidden = x.classList.contains("hidden") if (ishidden == true) { x.classList.remove("hidden"); x.classList.add("shown"); moreText.innerHTML = "Show less"; } else { x.classList.remove("shown"); x.classList.add("hidden"); moreText.innerHTML = "Show more"; } }
 div { width: 100px; height: 100px; } .hidden { display:none } .shown { display:block; }
 <button id="csrmore" onclick="myFunction()"> Show more </button> <div id="myDIV" class="hidden"> This is the triggerable content. </div>

Violín: https://jsfiddle.net/6zxa0Lg2/

Funciona bien, sin embargo, dado que soy un iniciador de JS, me preguntaba si esto es una mala práctica o es un código totalmente bueno.

Gracias por cada ayuda :)

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

0

Aquí hay otra manera de hacerlo. Hazlo todo relativo. Se hace clic en el botón y el javascript encuentra el contenido asociado a ese botón para mostrar/ocultar. De esta manera, no necesita ninguna etiqueta de identificación y puede tener tantos botones mostrar/ocultar como desee en la página.

 document.addEventListener('DOMContentLoaded', () => { // after the page loads... document.querySelectorAll('.csrmore').forEach(button => { // find all the 'show more' buttons and for each one... button.addEventListener('click', e => { // when someone clicks this button let content = e.target.closest('.container').querySelector('.content'); // find the content div associated with this button content.classList.toggle('hidden'); // toggle on or off the content e.target.innerText = content.classList.contains('hidden') ? 'Show more' : 'Hide'; // change the text of the button }) }) })
 div { width: 100px; height: 100px; } .hidden { display: none }
 <div class='container'> <button class="csrmore"> Show more </button> <div class="content hidden"> This is the triggerable content. </div> </div> <hr> <div class='container'> <button class="csrmore"> Show more </button> <div class="content hidden"> This is the triggerable content. </div> </div>

about 4 years ago · Juan Pablo Isaza Report

0

¡Esta es una buena manera de hacer esto! Esta no es la solución que no se me hubiera ocurrido, pero en realidad es bastante inteligente. Hubiera pensado hacerlo cambiando TARGET.style.visibility a "oculto" o "visible" al hacer clic en el botón. Sin embargo, de nuevo, ¡tu código se ve perfectamente bien!

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!