How to change CSS style of an Element?

233 views
Skip to first unread message

Rok Kralj

unread,
Dec 25, 2016, 10:50:38 AM12/25/16
to Scala.js
This is valid JS, but it does not compile with Scala.JS:

dom.document.querySelector("#element").style.display = "block";

Why not and how can I do it otherwise?

Justin du coeur

unread,
Dec 25, 2016, 11:00:48 AM12/25/16
to Rok Kralj, Scala.js
This one works in JavaScript precisely because JavaScript is so slipshod about types; unfortunately, the DOM API sometimes doesn't quite make sense when you look at it more carefully.

The problem is basically that querySelector() officially returns an Element, but what you need is an HTMLElement.  I don't know of an elegant solution to this one yet (although I haven't been paying close attention to this question), but I believe this tweak should work:

dom.document.querySelector("#element").asInstanceOf[HTMLElement].style.display = "block";

Basically, the asInstanceOf here says, "yes, I know that the API officially just says that this is an Element, but *I* know that it's actually an HTMLElement, so take my word for it".  One of these days, we might figure out a more elegant approach...

--
You received this message because you are subscribed to the Google Groups "Scala.js" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scala-js+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/scala-js/838aece1-ada1-405b-ae34-d089c2949eb9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages