I am trying to apply a JS script (a 'Select all' box) to a dynamically rendered UI, but the following does not work:
library(shinyjs)
ui <- fluidPage(
useShinyjs(),
uiOutput("checkbox_ui")
)
server <- function(input, output, session) {
output$checkbox_ui <- renderUI({
checkboxGroupInput(inputId = "myCheckbox",
label = NULL,
choices = c("All", "A", "B"))
})
runjs(HTML("
$(\"#myCheckbox input[value='All']\").click(function(e) {
$(\"#myCheckbox input[type='checkbox']\").prop('checked', $(e.target).prop(\"checked\"));
});
"))
}
shinyApp(ui, server)
However, when I skip using renderUI, the JS script is ran:
library(shinyjs)
ui <- fluidPage(
useShinyjs(),
checkboxGroupInput(inputId = "myCheckbox",
label = NULL,
choices = c("All", "A", "B"))
)
server <- function(input, output, session) {
runjs(HTML("
$(\"#myCheckbox input[value='All']\").click(function(e) {
$(\"#myCheckbox input[type='checkbox']\").prop('checked', $(e.target).prop(\"checked\"));
});
"))
}
shinyApp(ui, server)
Would someone know a workaround?
