Hi Lukas, don't worry about the range thing. Happens to all of us. 🙂
On this question, it depends on the context. I assume you mean having the string in your brython script, and wanting to add it to the page in rendered form (e.g. ☑️ not ✓).
In which case two ways come to mind. Neither are particularly pythonic.
The simplest is to replace the .innerHTML property of an existing element. Whatever string you set as innerHTML, the browser will parse as html before inserting in the document:
node = document ['foo']
node.innerHTML = '✓'
This approach is crude but effective. It replaces all the html children of node with your new string.
Sometimes you don't want to replace the existing children. Sometimes you want to swap out one node. In this case, you first need to turn your string into an html tag, then insert it in the DOM.
JS provides the DOMParser object to turn strings into DOM nodes. Here's a wrapper function I wrote for brython:
def make_html (text) :
'Turn a string into DOM object'
# see https://stackoverflow.com/questions/10585029/parse-an-html-string-with-js
parser = window.DOMParser.new ()
html = parser.parseFromString (text, 'text/html')
return html.body.firstElementChild
The last line is needed because DOMParser returns a complete html document - which means it wraps your html elements in <html> <body> tags. My function assumes a string only has a single top-level html element (a string with an html escape sequence like ✓ inside is a single element). Nested tags are ok (<table><tr><td></td></tr></table>). If you have multiple top-level elements then you need to alter the last line.
Once you have a DOM element, just insert it in the page:
newnode = make_html (mytext)
document ['foo'].appendChild (newnode)
Or you can add it the brythonic way: document ['foo'] <= newnode
Personally I try to avoid brython-specific DOM manipulation like <= so my code has some degree of portability. But either works perfectly fine.
HTH,
Ed