Last Updated: February 25, 2016
·
3.658K
· timgluz

Handling events with Clojurescript Dommy

Dommy - it's kickass Dom library for clojureScript, but it still has very minimal documentation, specially misses examples for events.

Here's my little workflow with examples how register events and how to write proper event handlers by using dommy.

```clojurescript
(ns foxyeye.search
  (:use-macros
     [dommy.macros :only [sel sel1]])
  (:require [dommy.core :as dommy]))

;; it takes value from input field and prints out on console
(defn on-search [ev]
  (let [q (dommy/value (sel1 :#search-query))]
    (.log js/console q)))

;; it marks selected list item as selected
(defn toggle-facets-item [ev]
    (.log js/console ev) ;;show how ev object looks alike
    (dommy/toggle-class! (.-currentTarget ev) "selected"))

(defn ^:export init []
  (.log js/console "Search app is initialized")
 ;; register event for single item
  (dommy/listen! (sel1 :#search-btn)
                 :click on-search)
;; register event for multiple items once
  (doseq [item (sel :.facets-language-item)]
    (dommy/listen! item :click toggle-facets-item)))
```

1 Response
Add your response

Nice post. I wonder why the dommy readme doesn't still have this. Maybe you should open a pull request?

over 1 year ago ·