Black Lives Matter. Please consider donating to Black Girls Code today.

Changing modebar button attributes

I made several custom modebar buttons, only for 1 button I would like to download something. In order to do this, I would like to set the download and href attribute of the modebar button itself. But this is inconvenient when using the “modeBarButtonsToAdd” function because I guess the element is not created yet.

What I can do is afterwards add a button myself manually, but this is ugly. Is there anyway to do this using modeBarButtonsToAdd function provided by the plotly.js api?

My final goal is actually to save the plot as a html file. Thus given a plot with id, then the button a attribution should have

a.setAttribute(“download”, “plot.html”);
a.setAttribute("href ", “data:text/html,” + document.getElementById(id).innerHTML);

I’d recommend making your button handler create its own invisible download button.

See how we implement our save as png button here:

Well, this was great help. I actually did not know that you can make a temporary “a” element to download stuff. I quickly implemented it and it works.

Again, thx for your help :slight_smile: