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

308
Visualizações
Change innerHTML of element ,javascript Closest return null

There is an element by class name unlock and by on click event the innerHTML of that will be changed by another svg icon. it works good and replaced by new svg icon.

<div class="Panel card" id="leftSidebarForm">
        <div class="Panel card-subtitle">
            <div class="column">
                <div class="unlock">
                    <svg>1</svg>
                </div>
                <div class="Panel">
                    <svg>1</svg>
                </div>
                <div class="unchain">
                    <svg>2</svg>
                </div>
                <div class="Panel">
                    <svg>3</svg>
                </div>
            </div>
        </div>
    </div>

But I define an EventListener on document to close the above element when click on outside.

document.addEventListener("click", function (event) {
    
    if (document.getElementById('leftSidebarForm') != null &&
        !event.target.closest('#leftSidebarForm')  &&
        !event.target.closest('#left-sidebar-menu')
    ) {

      // do something

    }
});

The problem occur by click on svg 1 to change the icon, it close the leftSidebarForm while it is its parent and must not close the leftSidebarForm. By click on svg icon to change its content this snippet code return null

event.target.closest('#leftSidebarForm')

I think it's because by changing innerHTML of element we remove it from dom and event.target.closest('#leftSidebarForm') return null because that svg not exist.

How can I solve that problem ?

about 4 years ago · Juan Pablo Isaza
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