manu p
manu p

Reputation: 985

passing a variable to JS in shiny

Below is the sample action button with JS in it.This is hard coded. But actually, the 'small_ID' below is very dynamic and will get changed. So how can i pass a variable inside document.getElementById ? Can anyone help?

actionButton("show1",class = "act_button", list(span(id = "m1", class="top left", "Model 1"), span(class="top right", Sys.time())),
                         onclick = "Shiny.setInputValue('btnid', document.getElementById('small_ID').textContent);")

Upvotes: 0

Views: 116

Answers (2)

trincot
trincot

Reputation: 349956

You could do this with paste:

actionButton(
    "show1",
    class = "act_button", 
    list(
        span(id = "m1", class="top left", "Model 1"), 
        span(class="top right", Sys.time())
    ),
    onclick = paste(
       "Shiny.setInputValue('btnid', document.getElementById('",
       small_ID,
       "').textContent);",
       sep=""
    )
)

...where small_ID should be your variable holding the id.

Upvotes: 1

Raine Revere
Raine Revere

Reputation: 33597

You can insert variables into strings using template literals:

actionButton("show1", class = "act_button", 
  list(
    span(id = "m1", class="top left", "Model 1"), 
    span(class="top right", Sys.time())
  ),
  onclick = `Shiny.setInputValue(
    'btnid', document.getElementById('${dynamicId}').textContent);
  `)

Upvotes: 0

Related Questions