deadghost
deadghost

Reputation: 5227

Find value of input element

I thought this would be pretty straight-forward but it seems like I'm overlooking something.

(ns main.core
  (:require [dommy.core :refer-macros [sel sel1]]))

(sel1 :#my-input) => #<[object HTMLInputElement]>

(.value (sel1 :#my-input)) 
=> #<TypeError: document.querySelector(...).value is not a function>

Upvotes: 3

Views: 1587

Answers (2)

Rodrigo Taboada
Rodrigo Taboada

Reputation: 2727

In ClojureScript we have two special forms for interop with JavaScript: . and .-.

. should be used to call methods of objects and .- should be used to access properties (including functions as a value). If you look at the source code of the value function in the Dommy library you'll see that it uses the .- special form.

Take a look here: https://github.com/clojure/clojurescript/wiki/Differences-from-Clojure#host-interop

This cheatsheet is also useful: https://himera.herokuapp.com/index.html

Upvotes: 4

deadghost
deadghost

Reputation: 5227

While I'm not 100% why .value isn't working, the following works splendidly:

(ns main.core
  (:require [dommy.core :as dommy :refer-macros [sel sel1]]))

(sel1 :#my-input) => #<[object HTMLInputElement]>

(dommy/value (sel1 :#my-input)) 
=> "my-input-value"

Upvotes: 2

Related Questions