Set a Molfile Dynamically

To update the molecule displayed by a component after page load, use the #setMolfile function. The example below uses #setMolfile together with #getMolfile to copy the molecule in an Editor to an Image. Changes are triggered by the user changing the Editor's molecule.

<!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; margin-bottom: 1em; }
      .image { width: 500px; height: 200px; background-color: #e0e0e0; border-radius: 3px;}
    <div class="editor">
      <div id="editor" data-chemwriter-ui="editor"></div>
    <div class="image">
      <div id="image" data-chemwriter-ui="image"></div>
      chemwriter.System.ready(() => {
        let editor = chemwriter.components.editor;

        editor.addEventListener('document-edited', (e) => {
          let image = chemwriter.components.image;


Set a Molfile Dynamically