Home > Mobile >  Jquery inside observeEvents
Jquery inside observeEvents

Time:07-16

I am trying to use Jquery inside observeevent but not able to execute it. The moment I click on "Release" button , the side bar should open. I have a Jquery here but not working. But when I put the same jquery inside onclick method, it works. But not in this method

library(shiny)
library(shinydashboard)
library(shinyjs)

ui <- dashboardPage(
  dashboardHeader(title = "Basic dashboard"),
  dashboardSidebar(collapsed = TRUE),
  dashboardBody(
    # Boxes need to be put in a row (or column)
actionButton("release", "Release"))
    )


server <- function(input, output) {

  useShinyjs()
  observeEvent(input$release,{
    tags$head(tags$script("$('body').toggleClass('sidebar-collapse');"))
  })
}

shinyApp(ui, server)

CodePudding user response:

Move useShinyjs() to the dashboardBody (with a comma), and use the toggleClass function:

# in ui
dashboardBody(
    useShinyjs(),
    actionButton("release", "Release"))
)

# in server
observeEvent(input$release, {
    toggleClass(selector = "body", class = "sidebar-collapse")
})
  • Related