get selected value of a select box in Clojure
Table of Contents
option onclick Event not Supported
With a recent bug I was reminded that <option />
are not allowed to have onClick events by Google Chrome. This led to some annoying debugging as to why something was working in Firefox but not Chrome on one of our applications. Fortunately, Clojure ships with Google Closure, which provides a straight-forward way of obtaining the selected index, and then getting that from the options of the sel.
(ns example-with-closure
"Goog items are included in clojurescript, so no need to add any deps."
(:require [goog.dom :as dom]))
(defn get-selected-value
"Get the currently selected value of a select drop-down.
This can be fired from the on-change event of the :select item"
[select-id]
(let [sel (dom/$ select-id)
x (.-selectedIndex sel)]
(.-value (aget (.-options sel) x))))