Ishan
Ishan

Reputation: 1006

Using variables in js.eval() function in scala js

I am using javascript to fetch data from a text box using js.eval() function like :

val amount=js.eval("document.getElementById('amount').value;")

It is working fine when I am writing the id directly i.e. 'amount' in this case. But I want to create a function which will receive the id and then use the variable id. I tried using '$' sign also like :

def getAmount(amount_id:String):Unit={
     println("Amount is : "+js.eval(s"document.getElementById(${id}).value;"))

But it is not working. Any suggestions ?

Upvotes: 0

Views: 421

Answers (1)

sjrd
sjrd

Reputation: 22085

Do not use js.eval. Use the interoperability features of Scala.js instead:

import org.scalajs.dom
import dom.html

def getAmount(amount_id:String): Unit = {
  val input = dom.document.getElementById(amount_id).asInstanceOf[html.Input]
  println(s"Amount is : ${input.value}")
}

Upvotes: 2

Related Questions