Search code examples
rtextshinyonclickmouseevent

Shiny - Clear inside the text when clicked on it


I am trying to make an Shiny app which takes input from user in textInput. I want text inside the textbox to be clear when it is clicked on. I could find solutions only for clicked on button. I need a mouse event for clicking on text box.

Do you have any idea about it?


Solution

  • This can be achieved through the shinyjs onclick function like so:

    library(shiny)
    library(shinyjs)
    
    ui <- fluidPage(
        
        useShinyjs(),
        
        fluidRow(
            textInput(inputId = "text_input", label = "Example Text Input", value = "Click me to clear")
        )
    )
    
    server <- function(input, output) {
        
        shinyjs::onclick(id = "text_input", expr = updateTextInput(inputId = "text_input", value = ""))
        
    }
    
    # Run the application 
    shinyApp(ui = ui, server = server)