Fork me on GitHub

Hi, how to change the content of one element of HTMLCollection using ClojureScript? I just want to translate something like document.getElementsByTagName('style')[0].innerHTML="xxx"; to ClojureScript.


(let [el (-> (js/document.getElementsByTagName "style")
             (aget 0))]
  (set! (.-innerHTML el) "xxx"))

👆 5
👍 5

also (aset (js/document.getElementsByTagName "style") 0 "innerHTML" "xxx")


yeah but that is abusing aset for something it is not supposed to do since innerHTML is not an array


aset really should only have numeric indexes as args with a final value


looking at the call to js* I don't think it would be a problem


(js* "(~{}[~{}][~{}] = ~{})" (js/document.getElementsByTagName "style") 0 "innerHTML" "xxx")


might not be the intended purpose but...


that is not the point. yes it generates the correct code since it happens to be the same JS code


I did this: (let [xx (some-> js/document (.getElementsByTagName "style") array-seq first .-innerHTML)] .. ) ... and when xx is not empty, do (set! xx "xxx")


but that won't work? xx will be a string so using set! on it won't update the actual element


Thanks, changed to (let [xx (some-> js/document (.getElementsByTagName "style"))] ..) and then (set! (-> xx array-seq first .-innerHTML) "xxx")


I am exploring extend-via-metadata and I am getting this behavior:

(defprotocol Component
  :extend-via-metadata true
  (start [component]))

(def component (with-meta {:name "db"} {`start (constantly "started")
                                        `clojure.core.protocols/datafy (fn [x] (:name x))}))

(satisfies? clojure.core.protocols.Datafiable component)
;; => true
(satisfies? Component component)
;; => false
So when extending with datafy protocol i get true when checking if the data satisfies? the protocol. However with a custom protocol with :extend-via-metadata true I am getting false in the satisfies? check. Is there something I am missing in the implementation? thanks

Alex Miller (Clojure team)15:02:37

In Clojure, satisfies? doesn’t check metadata extensions, not sure about cljs

Jimmy Miller16:02:33

Just to expand on the answer as to why satisfies seems to work for Datafiable. Datafiable is implemented on Object. This even works.

(satisfies? clojure.core.protocols/Datafiable 2)
;; => true


thanks guys, makes sense.

Mario C.17:02:22

Is it okay to have multiple puts (`>!`) within a go block or should each put be within its own go-block? If the answer depends then what are some things I should consider before doing one over the other?

Mario C.18:02:05

@jkr.sw Thanks! I originally had each put within a go-block but I was thinking about it and reasoned that since, the way it was currently set up, everything was happening synchronously I might as well do them all within one go block. But wasn't sure if this was proper


I'm trying to write some code (to run at compilation time) that will only do a require if a library is present. I thought of trying something like this:

(defmacro include-if-present []
  (when (io/resource "foo/bar.cljc")
    '(require '
but it doesn't seem to work and the docs for cljs.core/require suggest it's 'for the repl only' .. not sure what restrictions that implies exactly. Any ideas please let me know.


CLJS doesn't have dynamic require in general


Has anyone gone through LearnReagent? I'm trying to build at the increments with no luck.