ChemWriter

Read a Molfile

A molfile can be obtained from a component with the #getMolfile function. A reference to the component can be obtained by first assigning an id attribute to the sacrificial element. The reference can then be obtained via the chemwriter.components object.

The following example shows how to display a molfile obtained from an Editor in response to the clicking of a button.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <title>Read Molfile from Editor | ChemWriter</title>
    <link rel="stylesheet" href="https://chemwriter.com/sdk/chemwriter.css">
    <script src="https://chemwriter.com/sdk/chemwriter.js"
            data-chemwriter-license="https://chemwriter.com/licenses/396f597e46fbdbd39e34108b010c6efcb2ef2ed2.txt">
    </script>
    <style>
      .editor { height: 350px; max-width: 500px; }
    </style>
  </head>
  <body>
    <div class="editor">
      <div id="my-editor"
           data-chemwriter-ui="editor"
           data-chemwriter-src="https://chemwriter.com/data/structure-5.mol">
      </div>
    </div>
    <button id="button" type="button">Get Molfile</button>
    <script>
      document.addEventListener('DOMContentLoaded', function () {
        let button = document.querySelector('#button');

        button.addEventListener('click', function (e) {
          let editor = chemwriter.components['my-editor'];

          alert(editor.getMolfile());
        });
      });
    </script>
  </body>
</html>

Run Example