Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to dispatch a click event #23

Open
murtaza52 opened this issue May 17, 2012 · 1 comment
Open

Unable to dispatch a click event #23

murtaza52 opened this issue May 17, 2012 · 1 comment

Comments

@murtaza52
Copy link

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

@neatonk
Copy link

neatonk commented Sep 8, 2014

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants