Listen for Edits

Responsive user interfaces can be enabled by listening to changes in editor state. The Editor #addEventListener function makes it possible to respond when the user changes the structure in the Editor window. Pass in the name of the event ("document-edited") and your function. Changes to the contents of the editor will trigger the execution of your function.

It's often convenient to configure listeners when a page loads, but only after ChemWriter finishes initialization. Use chemwriter.System.ready to add functions that will be run when ChemWriter is ready. For example, an alert can be displayed when the user edits a structure, as in the example below.

<!DOCTYPE html>
<html lang="en">
    <meta charset="utf-8">
    <title>Listen for Edits | ChemWriter</title>
    <link rel="stylesheet" href=""> 
    <script src="" data-chemwriter-license=""></script>
      .editor { max-width: 500px; height: 350px; }
    <div class="editor">
      <div id="editor" data-chemwriter-ui="editor"></div>
      chemwriter.System.ready(() => {
        let editor = chemwriter.components.editor;

        editor.addEventListener('document-edited', (e) => {
          alert('Document changed.');

Run Demo