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

304
Views
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
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!