Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set an attribute of a DOM element in Clojurescript?

I wish to set the "value" property of an "input" element using Clojurescript, but I am having trouble with the syntax of setProperties in goog.com. Has anyone got a working example?

Update ------

This seems to work:

(goog.dom.setProperties 
 (goog.dom/getElement "element-name")
 (clj->js {:value "text"}))
like image 510
yazz.com Avatar asked Jul 09 '13 10:07

yazz.com


1 Answers

If you need to create throwaway JS objects for use with JS APIs, you can do so directly using js-obj:

(js-obj "value" "text")
;; produces {"value": "text"} in the compiled output

Of course if you already have a ClojureScript map with the appropriate entries, clj->js will be more convenient.

More importantly, you might want to consider switching to a ClojureScript library for DOM manipulation. Several are available:

  • Luke VanderHart's Domina, which might have been the first one, is used by Enfocus (listed below) and Pedestal;

  • Prismatic's dommy, notable due to its own merits as well as the very entertaining blog posts about it on Prismatic's blog (which can serve as a great introduction to the benefits of macros: first one, second one, third one);

  • Creighton Kirkendall's Enfocus, which is in a nutshell an Enlive-like library for ClojureScript, which is awesome;

  • Kevin Lynagh's Singult, which is a Hiccup-style library for ClojureScript with cool functionality for merging in changes to the DOM, rather than rerendering from scratch.

like image 151
Michał Marczyk Avatar answered Sep 30 '22 18:09

Michał Marczyk