0

I am using doto to assign properties of a Dom element in ClojureScript, as below:

(let [img
          (doto (.createElement js/document "img")
            (set! -src "bunny-512.png")
            (set! -height 64)
            (set! -width 64))]
;...
)

Is there a way to do set all the properties at once via a map of properties+values instead?

Was thinking something like:

(let [img (.createElement js/document "img")]
  (set! img {:src "bunny-512.png" :height 64 :width 64})

But that does not work ...

1

1 Answer 1

1

Try something like this:

(defn set-props [o property-map] 
  (doseq [[k v] property-map] 
    (.setAttribute o (name k) v)))

Test:

(def mg (.createElement js/document "img"))

cljs.user=> (set-props mg {:src "foo.png" :height 128 :width 128})
nil

cljs.user=> (.-src mg)
".../foo.png"

cljs.user=> (.-width mg)
128

cljs.user=> (.-height mg)
128
Sign up to request clarification or add additional context in comments.

1 Comment

I am guessing no function nor macro to do this from core ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.