Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

d3 set image visibility=hidden onerror

How do I hide an svg:image when onerror is fired?

node.append('svg:image')
    .attr("x", "-30px")
    .attr("y", "-40px")
    .attr("width", "60px")
    .attr("height", "80px")
    .attr("xlink:href", function(d){
        return 'https://somedomain.com/images/' + d.name;
    })
    .attr("onerror", function(d){
      console.log('The image ' + d.name + ' failed to load');
      // this.style("visibility", "hidden");
      // d.style("visibility", "hidden");
    });
like image 353
Cathal Coffey Avatar asked Apr 13 '26 02:04

Cathal Coffey


1 Answers

Based on your description, the code would look something like this:

.on("error", function() {
  d3.select(this).style("visibility", "hidden");
});

That is assuming that the element you want to hide fires error events.

like image 187
Lars Kotthoff Avatar answered Apr 14 '26 14:04

Lars Kotthoff



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!