Introduction
This page presents the JavaScript API for the smath package. For this reason smath-js project has been created. You won't need to run GWT to make use of this functionality.
Details
Firstly, you need to add the source js file of smath-js app:
```
```
Then, in your js code, you can create an instance of smath object by calling smathCreateInstance(id, expr)
function. This function shouldn't be called unless smathOnAppLoaded()
function is called. smathOnAppLoaded()
indicates, that smath-js engine is ready. You may need to check id the smath engine is ready before creating new smath instance.
``` var smathReady = false; function smathOnAppLoaded(){ smathReady = true; }
/** * @param id the id of the element (ex. div) that will parent the * @param expression the expression string (ex. "2+2=4") * @returns reference to JavaScript object that enables the interaction with the smath object (ex. accessing the gaps) */ var currentSmathInstance; function addInstance(id, expression){ if (smathReady) currentSmathInstance = smathCreateInstance(id, expression); } ```
Then, somewhere else in the code, you may access the contents of the smath object though the reference taht were returned in the example above. The below code retrieves and displays the current text of the first gap.
``` // get text for the selected gap currentSmathInstance.getGapTextAt(0);
// set text for selected gap currentSmathInstance.setGapTextAt(0, "1");
// get gaps count in the smath expression object currentSmathInstance.getGapsCount();
// get selected gap element (TextInput) var el = currentSmathInstance.getGapElementAt(0); var text = el.value; ```
Example
You can look up the example by visiting this page: http://smath-js2.appspot.com/. Feel free to analyze the page source.