How do I get around TypeError: Cannot read property ... of null\ when I have a function that supplies data to a cell with a specific ID value using \document.getElementById():?
The table will work on play, and my console log is not returning any errors about promises or the like.
Unless I am mistaken, this question hearkens back to @bgchen’s answer to a similar problem, but I still can’t work my way through it. I’ve tried combinations of fetch and .then, but I can’t seem to work out the syntax… none of the spots where I try to close the fetch statement work. I’ve also tried adding in ‘await’ in a few spots, but these doesn’t help.
The examples work fine in HTML, so I’m assuming this has something to do with how Observable determines cell run order?
I’ll read about Helper Functions to see if I can figure out how to use them – and how this differs from the solution you modeled (which is to reference the DOM element directly?).
Those helpers are snippets that utilize vanilla JS without the help of external libraries. Since you’re already on Observable you can use the helpers from Observable’s Standard Library (this showcases the second point in my previous answer):
Another side note: There’s rarely ever any need to use Element.innerHTML. You’ll generally want to assign to Element.textContent which will automatically escape the assigned string. This is especially important for data that is not controlled by you.
It’s less of an issue on Observable (because the iframe is sandboxed), but missing this step in your own scripts can lead to XSS vulnerabilities.
tl;dr: If you don’t know wether a string contains HTML markup, don’t use innerHTML or Observable’s html`…` template tag. Instead create the element first, then assign to textContent.
Thanks again. A couple of days ago I also came across this MDN discussion of Node.textContent, but didn’t fully get it . Your note here boils it down so much better!
You can safely embed arbitrary text content inside of an html tagged template literal by using an embedded expression and a text node, such as using DOM.text. For example:
html`This is text: ${DOM.text("<marquee>hello</marquee>")}`