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

262
Visualizações
Uncaught TypeError: Cannot read properties of undefined (reading 'stopPropagation')

stopPropagation() is not working in my code. What is the issue?

When you will click on button then a dropdown will toggle and I want to hide this toggle when user click on anywhere on page.

var iconListBoxBody;

function btnCrypto(e) {
  e.stopPropagation();
  iconListBoxBody = document.getElementById("iconListBoxBody");
  iconListBoxBody.classList.toggle("active");
}

document.body.addEventListener("click", function() {
  iconListBoxBody.classList.remove("active");
});
.iconListBoxBody {
  display: none;
}

.iconListBoxBody.active {
  display: block;
}
<div class="iconListBox">
  <div class="iconListBoxHeader">
    <img src="assets/images/coin1.png" alt="">
    <input type="text" class="form-control" value="BTC" />
    <button class="btn-crypto btn btn-primary" onclick="btnCrypto()"><i class="fa fa-chevron-down"></i>click</button>
  </div>
  <div class="iconListBoxBody" id="iconListBoxBody">
    <ul class="iconListBoxBodyList">
      <li>
        <img src="assets/images/coin1.png" alt="">
        <span class="coinText">BTC</span>
      </li>
      <li>
        <img src="assets/images/coin2.png" alt="">
        <span class="coinText">ETH</span>
      </li>
    </ul>
  </div>
</div>

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

0

Added JavaScript event listeners.

var iconListBoxBody;

function btnCrypto(e) {
  e.stopPropagation();
  iconListBoxBody = document.getElementById("iconListBoxBody");
  iconListBoxBody.classList.toggle("active");
}

document.body.addEventListener("click", function() {
  iconListBoxBody.classList.remove("active");
});

document.querySelectorAll(".btn-crypto").forEach(el => (el.onclick = btnCrypto))
.iconListBoxBody {
  display: none;
}

.iconListBoxBody.active {
  display: block;
}
<div class="iconListBox">
  <div class="iconListBoxHeader">
    <img src="assets/images/coin1.png" alt="">
    <input type="text" class="form-control" value="BTC" />
    <button class="btn-crypto btn btn-primary">
    <i class="fa fa-chevron-down"></i>click</button>
  </div>
  <div class="iconListBoxBody" id="iconListBoxBody">
    <ul class="iconListBoxBodyList">
      <li>
        <img src="assets/images/coin1.png" alt="">
        <span class="coinText">BTC</span>
      </li>
      <li>
        <img src="assets/images/coin2.png" alt="">
        <span class="coinText">ETH</span>
      </li>


    </ul>
  </div>
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

You just forgot to pass event param, I changed onclick="btnCrypto()" to onclick="btnCrypto(event)":

var iconListBoxBody;

function btnCrypto(e) {
  e.stopPropagation();
  iconListBoxBody = document.getElementById("iconListBoxBody");
  iconListBoxBody.classList.toggle("active");
}

document.body.addEventListener("click", function() {
  iconListBoxBody.classList.remove("active");
});
.iconListBoxBody {
  display: none;
}

.iconListBoxBody.active {
  display: block;
}
<div class="iconListBox">
  <div class="iconListBoxHeader">
    <img src="assets/images/coin1.png" alt="">
    <input type="text" class="form-control" value="BTC" />
    <button class="btn-crypto btn btn-primary" onclick="btnCrypto(event)"><i class="fa fa-chevron-down"></i>click</button>
  </div>
  <div class="iconListBoxBody" id="iconListBoxBody">
    <ul class="iconListBoxBodyList">
      <li>
        <img src="assets/images/coin1.png" alt="">
        <span class="coinText">BTC</span>
      </li>
      <li>
        <img src="assets/images/coin2.png" alt="">
        <span class="coinText">ETH</span>
      </li>
    </ul>
  </div>
</div>

about 4 years ago · Juan Pablo Isaza Relatório

0

Since you're using addEventListener already, remove the inline JS calling the btnCrypt function, and use addEventListener to add it to the cached button element.

// Cache your elements
const iconListBoxBody = document.querySelector('.iconListBoxBody');
const button = document.querySelector('.btn-crypto');

function btnCrypto(e) {
  e.stopPropagation();
  iconListBoxBody.classList.add('active');
}

document.body.addEventListener('click', function() {
  iconListBoxBody.classList.remove('active');
});

button.addEventListener('click', btnCrypto);
.iconListBoxBody { display: none; }
.active { display: block; }
<div class="iconListBox">
  <div class="iconListBoxHeader">
    <img src="assets/images/coin1.png" alt="">
    <input type="text" class="form-control" value="BTC" />
    <button class="btn-crypto btn btn-primary">
    <i class="fa fa-chevron-down"></i>click</button>
  </div>
  <div class="iconListBoxBody" id="iconListBoxBody">
    <ul class="iconListBoxBodyList">
      <li>
        <img src="assets/images/coin1.png" alt="">
        <span class="coinText">BTC</span>
      </li>
      <li>
        <img src="assets/images/coin2.png" alt="">
        <span class="coinText">ETH</span>
      </li>
    </ul>
  </div>
</div>

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