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

305
Vistas
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 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