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

167
Views
Stopping onclick from propagating "through" other elements

In this codepen, clicking on a node causes a popover to appear. Eventually, I want links and clickable things to be in the popover. Problem is, right now when you click it, the popover closes.

I'm using an onclick event so when you click anywhere (HTML body), the popover closes. I prefer this over say an "x" close button in the popover, but I don't know how to uninclude the popover in this click to close event.

d3.select("body").on("click", function() {
  console.log("click on body")
  tooltip.style("opacity", 0)
  tooltip.style("visibility", "hidden")
})

I thought maybe the d3 stopPropagation event on the "tooltip" would somehow come into play here, but I'm not sure.

Thanks for any help!

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

The way most websites handle this is by adding both a popover and a background overlay, and then only registering clicks to the background overlay.

For example:

d3.select("circle")
  .on("click", clickNode);

var tooltipBg = d3
  .select(".tooltip-overlay")
  .on("click", clickBackground);
var tooltip = d3.select(".tooltip");

function clickNode(event) {
  const [x, y] = d3.pointer(event);
  
  tooltipBg.style("display", "block");
  
  tooltip
    .style("left", `${event.layerX}px`)
    .style("top", `${event.layerY}px`)
    .style("display", "block")
    .transition()
    .duration(300)
    .style("opacity", 1);
}

function clickBackground() {
  tooltipBg.style("display", "none");
  
  tooltip
    .transition()
    .duration(300)
    .style("opacity", 0)
    .on("end", () => tooltip.style("display", "none"));
}
.tooltip-overlay {
  position: absolute;
  width: 100vw;
  height: 100vh;
  top: 0;
  left: 0;
  opacity: 0;
  display: none;
}

.tooltip {
  position: absolute;
  padding: 12px;
  z-index: 10;
  border: solid 2px red;
  width: 200px;
  height: 100px;
  background-color: white;
  border-radius: 5px;
  box-shadow: 6px solid #eee;
  opacity: 0;
  display: none;
}
<script type="text/javascript" src="https://d3js.org/d3.v7.min.js"></script>

<svg height="500" width="500">
  <circle r="20" cy="50" cx="50"></circle>
</svg>

<div class="tooltip-overlay"></div>
<div class="tooltip"></div>

about 4 years ago · Santiago Gelvez 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!