Skip to content

Instantly share code, notes, and snippets.

@oakmac
Last active March 31, 2021 00:43
Show Gist options
  • Save oakmac/1acaaa380820a599278e0a77f362761e to your computer and use it in GitHub Desktop.
Save oakmac/1acaaa380820a599278e0a77f362761e to your computer and use it in GitHub Desktop.
dom helpers functions
;; this code licensed as CC0 1.0 Universal (SPDX: CC0-1.0)
(ns ^{:doc "DOM helpers"
:author "Chris Oakman"}
com.oakmac.some-project.util.dom
(:require
[goog.dom :as gdom]
[goog.dom.forms :as gforms]))
;; TODO: this could be variadic with some options
(defn form->values
"Returns the values from a DOM Form Element as a ClojureScript Map"
[el]
(let [el (gdom/getElement el)
form-data-map (gforms/getFormDataMap el)
form-data-obj (.toObject ^goog.structs.Map form-data-map)
form-data (js->clj form-data-obj)]
(reduce
(fn [acc [k itm]]
(assoc acc (keyword k) (first itm)))
{}
form-data)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment