Marsenau
Marsenau

Reputation: 1155

Update shiny input from java script

I've looked around and tried a few things but I can't seem to get it to work. I want to be able to update the value stored in input$someVar from a bit of js code. What I need to on a certain element click, I need to update the input$someVar value. I know there are functions like "updateSelectInput()" but those are called from the server.

I can physically change the value of the data in the HTML that is used int he data attribute and that is displayed, but the server doesnt see this as a change and the input$someVar stays the same.

I have tried

var selectBind = Shiny.inputBindings.bindings[5];

selectBind.binding.setValue('#loc', newValue);

within an event handler, where #loc is the id of the input element, in hope that I could do it that way but this gives me an error.

Is there a way to do the functionality of "updateSelectInput()" within java script in the ui?

Upvotes: 5

Views: 1339

Answers (1)

Geovany
Geovany

Reputation: 5677

Yes, there is a way. Use the JavaScript function Shiny.onInputChange.

  // change the value of an input
  document.getElementById("id").value = "value";
  // report the change to shiny
  Shiny.onInputChange("id", "value");

Upvotes: 6

Related Questions