I'm having a strange problem with a bit of Clojurescript in my application. I pulled all the relevant pieces out into a small Leiningen Reagent application. Here's the relevant Hiccup:
(defn current-page []
(fn []
(let [page (:current-page (session/get :route))]
[:div
[:table {:width "100%"}
[:tbody
[:tr {:class "row"}
[:td "A"] [:td "B"] [:td "C"]]
[:tr {:class "row"}
[:td "A"] [:td "B"] [:td "C"]]
[:tr {:class "row"}
[:td "A"] [:td "B"] [:td "C"]]
[:tr {:class "row"}
[:td "A"] [:td "B"] [:td "C"]]
[:tr {:class "row"}
[:td "A"] [:td "B"] [:td "C"]]
[:tr {:class "row"}
[:td "A"] [:td "B"] [:td "C"]]
[:tr {:class "row"}
[:td "A"] [:td "B"] [:td "C"]]]]
[:div {:onClick #(unselect-all-rows)} "Unselect All Rows..."]
[:div {:onClick #(select-all-rows)} "Select All Rows..."] ])))
It's just a simple HTML table. Here are the select and unselect functions:
(defn unselect-all-rows []
(prn "Unselecting all.")
(let [selected-rows (.getElementsByClassName js/document "row-selected")]
(prn (str "Rows selected: " (.-length selected-rows)))
(doall (map #(.remove (.-classList %) "row-selected") (array-seq selected-rows)))))
(defn select-all-rows []
(prn "Selecting all.")
(let [selectable-rows (.getElementsByClassName js/document "row")]
(doall (map #(.add (.-classList %) "row-selected") (array-seq selectable-rows)))))
The select-all-rows function works as expected, all rows get the "row-selected" class applied and the CSS highlights the rows:
But when I execute the unselect-all-rows function I only get SOME of the rows unselected:
If I click 2 more times then all the rows end up unselected. If I look at the console the number of rows being selected is what I expect, 7 in the first case, but it only seems to perform the remove operation on alternating rows:
What am I missing here?