Getting data from the dom.
To get data from the dom we use the from form. It comes in two basic forms listed below:
Single Extraction
(from a-node (extractor arg1 ...)) ;returns a list or single value ;or implied js/document as root node (from selector (extractor arg1 ...))
Multiple Extractions
(from a-node :key1 selector1 (extractor arg1 ...) :key2 selector2 (extractor arg1 ...)) ;returns a map {:key1 value|list :key2 value|list ;or implied js/document as root node (from :key1 selector1 (extractor arg1 ...) :key2 selector2 (extractor arg1 ...))
In the first case, from is passed a node/node set or selector and a extractor. It then calls the extractor on each element in the node set. It will return either a single value if a single node is passed in and a collection of values if more than one node is passed in.
An extractor is nothing more than a function that takes a set of arguments and returns a function that takes a set of nodes.
In the second case, we see from is optionally passed a node or node set and a set of key/selector/extractor trio. The selectors are scoped by the node or node set passed in and the results of each selector is passed on to its partner extractor and the result is stored in the return map under the given key.
The read-form extractor returns a the content of form as a clojure map. If more than one node is passed in it will return a list of values.
(read-form)
Example:
The following action is triggered when we click the button.
(defn read-form-demo [] (let [values (ef/from "#read-form-test" (ef/read-form))] (ef/at "#read-form-demo" (ef/content (pr-str values)))))
The get-prop extractor returns the specified property of the selected nodes. If more than one node is passed in it will return a list of values.
(get-prop :prop)
Example:
The following action is triggered when we click the button.
(defn get-prop-demo [] (let [values (ef/from js/document :field1 "#get-prop-field1" (ef/get-prop :value) :field2 "#get-prop-field2" (ef/get-prop :value) :field3 "input[name='get-prop-field3']" (ef/filter #(.-checked %) (ef/get-prop :value)))] (ef/at "#get-prop-demo" (ef/content (pr-str values)))))
The get-attr extractor returns the value or values of the selected nodes. If more than one node is passed in it will return a list of values.
(get-attr :attr)
Example:
The following action is triggered when we click the button.
(defn get-attr-demo [] (let [value (ef/from "#get-attr-img" (ef/get-attr :src))] (ef/at "#get-attr-demo" (ef/content (pr-str value)))))
The get-text extractor returns the text value or values of the selected nodes. If more than one node is passed in it will return a list of values.
(get-text)
Example:
The following action is triggered when we click the button.
(defn get-text-demo [] (let [txt (ef/from "#button3" (ef/get-text))] (ef/at "#get-text-demo" (ef/content txt))))