domina icon indicating copy to clipboard operation
domina copied to clipboard

Unable to dispatch a click event

Open murtaza52 opened this issue 12 years ago • 1 comments

Hi,

I am trying to dispatch a click event on a button -

(defn enter-to-click
  "It captures the enter event on the given element selector (s1) and triggers a click event on the element selector (s2)"
  [s1 s2]
  (let [el1 (sel s1) el2 (sel s2)]
    (listen! el1 :keydown (fn [ev]
                            (when (= (:keyCode ev) 13)
                              (dispatch! (sel "#sign_in") "click" {}))))))

The listen! event works, I am even able to log messages from the when block , however the dispatch doesnt work. I am however able to trigger a click event using jquery from the browsers console.

Any help will be appreciated.

Thanks, Murtaza

murtaza52 avatar May 17 '12 06:05 murtaza52

I know this is an old issue, so this comment is for the benefit of anyone else with the same question. As far as I can tell Google Closure Library does not provide a method for dispatching native browser events. There may be a better way, but here is the code I am using, at the moment to trigger a browser click event.

(defn click-event []
  (or (try (js/MouseEvent. "click" #js {:view js/window :bubbles true :cancelable true})
           (catch :default _))
      ;; NOTE: Fallback for Safari 5 in which `MouseEvent` is defined but broken.
      (doto (.createEvent js/document "MouseEvent") 
        (.initEvent "click" true true))))

(defn click [node]
  (if-not (fn? (.-click node))
    (.dispatchEvent node (click-event))
    (.click node)))

It should be possible to make this code generic enough to dispatch other browser events, but I have not had the need or the time to do so. If domina's dispatch! function is intended to support native browser events, I would be happy to take a stab at fixing it.

neatonk avatar Sep 08 '14 20:09 neatonk