Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

254
Visualizações
Trigger DIV content on button click + change button text

I am trying to trigger the visibility of a DIV via a button.

My code looks like this:

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>

Fiddle: https://jsfiddle.net/6zxa0Lg2/

It works fine, however since I am a JS starter, I was wondering if this is bad practice or is it a totally fine piece of code?

Thanks for every help :)

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Here's another way to go about it. Make it all relative. The button is clicked and the javascript finds the content associated to that button to show/hide. This way you don't need any ID tags and you can have as many show/hide buttons as you want on the page.

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 Relatório

0

This is a fine way to do this! This is not the solution I would not have come up with, but it is actually pretty clever. I would have thought to have done it by toggling TARGET.style.visibility to either "hidden" or "visible" when clicking the button. Again though, your code looks perfectly fine!

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda