Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use Clojurescript to interact with the html DOM?

I am new to Clojurescript. I want to know how to create html elements, and how to change their properties using clojurescript. I cannot seem to find a lot of relevant information online.

like image 638
mandy1339 Avatar asked Jul 21 '17 03:07

mandy1339


People also ask

How do I access the HTML DOM?

The easiest way to access a single element in the DOM is by its unique ID. You can get an element by ID with the getElementById() method of the document object. In the Console, get the element and assign it to the demoId variable. Logging demoId to the console will return our entire HTML element.

Can we use JavaScript to modify HTML DOM?

The HTML DOM allows JavaScript to change the content of HTML elements.

Which function can be used to access an HTML DOM element?

The getElementById Method The most common way to access an HTML element is to use the id of the element. In the example above the getElementById method used id="demo" to find the element.

Which method can be used by JavaScript to manipulate HTML elements in the DOM?

Using the innerHTML Property You can use the innerHTML property to add HTML elements to the DOM. This property is available on the document object and any element object that is a part of the DOM. The innerHTML property stores a string representation of the DOM's HTML.


2 Answers

ClojureScript provides good interoperability with JavaScript and the browser. You can call JavaScript globals and functions directly to query and manipulate DOM like so:

(-> js/document
    (.getElementById "app")
    (.-innerHTML)) ; returns contents of element with id 'app'

(-> js/document
    (.getElementById "app")
    (.-innerHTML)
    (set! "Hello Clojure!")) ; Sets new content for element

Checkout ClojureScript cheatsheet for short summary of JavaScript inter-operability in CLJS.

However, modern Web apps are not built with such direct DOM manipulation. @scott-lowe's answer suggests using Reagent, which is a ClojureScript bridge for React, (one of) the most popular JS framework for UIs at the moment. A good choice for getting started with Web apps.

In addition, I would recommend looking at re-frame, which builds on Reagent, and provides state management for your app. Easy way to get started is to install the Leiningen build tool, and simply say lein new re-frame my-app-name. This gives you a good template to start experimenting. Run the app with lein figwheel and you can see your changes to the cljs code instantly in the browser. A good place to start hacking is views.cljs, which contains hiccup-like template for your main app.

like image 119
Toni Vanhala Avatar answered Sep 30 '22 00:09

Toni Vanhala


Take a look at the Reagent project, which "provides a minimalistic interface between ClojureScript and React".

The Reagent project website includes this example:

(ns example
  (:require [reagent.core :as r]))

(defn simple-component []
  [:div
   [:p "I am a component!"]
   [:p.someclass
    "I have " [:strong "bold"]
    [:span {:style {:color "red"}} " and red "] "text."]])

(defn render-simple []
  (r/render [simple-component]
    (.-body js/document)))

In the above example, the Reagent component simple-component is rendered into the DOM at the body node of js/document. The Reagent component is just a simple function, but you can compose more complex blocks of HTML by composing these simple functions together.

There are many other ways to generate HTML and interact with the DOM from ClojureScript, but I think it's fair to say that Reagent is one of the most popular libraries in the ClojureScript community right now; it's also mature and well documented.

like image 44
Scott Avatar answered Sep 30 '22 00:09

Scott