Accessing DOM from EMSCRIPTEN

Viewed 1275

is there a way to access the DOM from a EMSCRIPTEN C++ application?

I'd like e.g., to read / set the value of an html textarea and receive html buttons onclick events.

Can someone provide a C++ snippet?

Thanks.

1 Answers

I'll try to answer my own question with the only method I found by now:

this is the html snippet:

<!-- html file -->
...
<input type="text" id="my_textbox" value="...">
...
<input type="submit" value="Submit" onclick="_onBtnPressed()">
...

and this is the C++ code:

// C++ file

// callback for button event
extern "C"
{
    void onBtnPressed() { std::cout << "Btn pressed\n"; }
}

...

// change text of a text box:
emscripten_run_script("document.getElementById('my_textbox').value = 'Hello, emscripten world!'");

Compiled with the flag:

emcc -s EXPORTED_FUNCTIONS="['_onBtnPressed']" ...

This method works. However, I'd expect some explicit emscripten API to directly manipulate the DOM.

Related