How do you have multiple javascript functions in RShiny? I'm combining example 1 and 2 from this example extendShinyjs: Extend shinyjs by calling your own JavaScript functions
jsCode <- '
shinyjs.backgroundCol = function(params) {
var defaultParams = {
id : null,
col : "red"
};
params = shinyjs.getParams(params, defaultParams);
var el = $("#" + params.id);
el.css("background-color", params.col);
}'
jsCode <- "shinyjs.pageCol = function(params){$('body').css('background', params);}"
shinyApp(
ui = fluidPage(
useShinyjs(),
extendShinyjs(text = jsCode, functions = c("backgroundCol")),
extendShinyjs(text = jsCode, functions = c("pageCol")),
selectInput("col", "Colour:",
c("white", "yellow", "red", "blue", "purple"))
p(id = "name", "My name is Dean"),
p(id = "sport", "I like soccer"),
selectInput("col", "Colour",
c("green", "yellow", "red", "blue", "white")),
selectInput("selector", "Element", c("sport", "name", "button")),
actionButton("button", "Go")
),
server = function(input, output) {
observeEvent(input$button, {
js$backgroundCol(input$selector, input$col)
})
observeEvent(input$col, {
js$pageCol(input$col)
})
}
)