I want to change the css class of a bootstrap button depending on the value of a checkbox. If the value of the checkbox (input$x
) is checked ie. euqals TRUE
, i want to add a class and when the value is false i want to remove the class.
The Code below is what ive tried but it does not work. Does someone has a clue why it does not work?
library(shiny)
library(tidyverse)
library(htmltools)
ui = fluidPage(
tags$style(
HTML(
".testclass {background-color: pink;")),
tags$div(style = "margin: 100px;",
tags$input(type = "checkbox", "Check", id = "x"),
tags$button("Click", class="btn btn-default dropdown-toggle", id = "dropdownMenu1"))
)
server = function(input, output, session){
### CUSTOM FUNCTIONS
observeEvent(input$x, {
if(isTRUE(input$x)){
tagQuery(ui)$find("button")$filter(function(x, i) tagGetAttribute(x, "id") == "dropdownMenu1")$addClass("testclass")
}else{
tagQuery(ui)$find("button")$filter(function(x, i) tagGetAttribute(x, "id") == "dropdownMenu1")$removeClass("testclass")
}
})
}
shinyApp(ui, server)