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

124
Views
event listener to close current dialog

The below code only works for the first dialog (BOB JONES) and the second dialog (ROBERT ABREAU) will open but not close onclick outside the dialog. I will have 150 more dialogs of text associated with student photos of a chess grandmaster. So IDK if I can use an event listener as per closeDialog() to close the current open dialog. Maybe a solution would involve getElementsByClassName. And I could patch that into the js code below, and omit the "onclick = "closeDialog();" on each dialog.

What's a good plan?

html

<!-- BOB JONES -->
<dialog id = "B-JONES" onclick = "closeDialog('B-JONES');">
<div class="test">
<p>Blah blah blah</p>
</div>
</dialog>
<!-- END BOB JONES

<!-- ROBERT ABREAU -->
<dialog id = "R-ABREAU" onclick = "closeDialog('R-ABREAU');">
<div class="test">
<p>Blah blah blah</p>
</div>
</dialog>
<!-- END ROBERT ABREAU -->

... 150 more

JS

function closeDialog(dialogID) {
 document.querySelector('dialog').addEventListener('click', function(e) {
if(!e.target.closest('div')) { e.target.close();}
 });
}
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Per your comment, the code you are looking for can be somewhere along these lines:

<!-- BOB JONES -->
<dialog id = "B-JONES" onclick = "closeDialog('B-JONES');">
<div class="test">
<p>Blah blah blah</p>
</div>
</dialog>
<!-- END BOB JONES

<!-- ROBERT ABREAU -->
<dialog id = "R-ABREAU" onclick = "closeDialog('R-ABREAU');">
<div class="test">
<p>Blah blah blah</p>
</div>
</dialog>
<!-- END ROBERT ABREAU -->

JS

 function closeDialog(dialogID) {
 let dialog = document.getElementById(dialogId);
 if (dialog) {
      dialog.close();
 }
}

Just make sure that the IDs of your dialogs are unique.

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