David
David

Reputation: 3046

How to create an onclick event in clojurescript?

html file:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
  </head>
  <body>
    <h1 id="app">This is clojure</h1>
  <button "test" onclick="hi()">hello</button>
    <script src="out/main.js" type="text/javascript"></script>
  </body>
</html>

hello world file: (ns hello-world.core)

(println "Hello world!")

(def hi []
(println "test")

  )

Should "hi" not get invoked in this clojure script project?

Upvotes: 0

Views: 711

Answers (1)

Thomas Heller
Thomas Heller

Reputation: 4356

No. Every reference in CLJS is namespaced. If anything it would be hello_world.core.hi() instead of just hi(). You should however never hook up events this way.

Instead change your HTML to <button id="test">hello</button> and then somewhere in your CLJS call this to add the actual event handler.

(let [el (js/document.getElementById "test")]
  (.addEventListener el "click"
    (fn [e]
      (println "test")))

Upvotes: 3

Related Questions