简体   繁体   中英

query an arbitrary html element, e.g. to see if it is disabled, with shinyjs

Can shinyjs do something like if(is_disabled("#my_element_id")) do_something() ? I'd like to be able to see if a specific HTML element is disabled (by shinyjs or other means) before doing something else with it.

There's no such function. The answer depends on what you want exactly. Here is something which could help:

library(shiny)
library(shinyjs)

jsCode <- 'shinyjs.isDisabled = function(params) {
  var el = $("#radiobtns");
  Shiny.setInputValue("disabled", el.prop("disabled"));
}' 

ui <- fluidPage(
  useShinyjs(),
  extendShinyjs(text = jsCode, functions = "isDisabled"),
  actionButton("button", "Disable radio buttons"),
  radioButtons("radiobtns", "Radio buttons", c("Yes", "No"))
)

server <- function(input, output) {

  observeEvent(input$button, {
    toggleState("radiobtns")
    js$isDisabled()
  })

  observeEvent(input$disabled, {
    if(input$disabled){
      cat("disabled\n")
    }else{
      cat("enabled\n")
    }
  })

}

shinyApp(ui = ui, server = server)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM