Including ID in Shiny JS module

My knowledge on JS is close to zero and I’m trying to modifying the answer of this question: Navigating using keys

I’m trying to implement the same in my Shiny app which uses modules, but I cant figure out where I should add the namespace in the code? The code below works, but does not react on the keystrokes.

js <- paste(
  "$(document).on('keydown', function(event){",
  "  var key = event.which;",
  "  if(key === 37){",
  "    Shiny.setInputValue('arrowLeft', true, {priority: 'event'});",
  "  } else if(key === 39){",
  "    Shiny.setInputValue('arrowRight', true, {priority: 'event'});",
  "  }",
  "});"
)

datasets <- list(mtcars, iris, PlantGrowth)

nameUI <- function(id) {
  ns <- NS(id)
  
       tagList(
      tags$head(tags$script(HTML(js))),
      mainPanel(
        titlePanel("Simplified example"),
        tableOutput(ns("cars")),
        actionButton(ns("prevBtn"), icon = icon("arrow-left"), ""),
        actionButton(ns("nextBtn"), icon = icon("arrow-right"), ""),
        verbatimTextOutput(ns("rows"))
      )
    )
  
}

nameServer <- function(id) {
  moduleServer(
    id,
    function(input, output, session) {
      
      output$cars <- renderTable({
        head(dat())
      })
      
      dat <- reactive({
        if (is.null(rv$nr)) {
          d <- mtcars
        }
        else{
          d <- datasets[[rv$nr]]
        }
      })
      
      rv <- reactiveValues(nr = 1)
      
      set_nr <- function(direction) {
        rv$nr <- rv$nr + direction
      }
      
      observeEvent(list(input$nextBtn, input$arrowRight), { 
        set_nr(1)
      })
      
      observeEvent(list(input$prevBtn, input$arrowLeft), { 
        set_nr(-1)
      })
      
      ro <- reactive({
        nrow(dat())
      })
      
      output$rows <- renderPrint({
        print(paste(as.character(ro()), "rows"))
      })
      
      vals <- reactiveValues(needThisForLater = reactive(30 * ro()))
      
    }
  )
}

app <- function() {
  ui <- fluidPage(
    nameUI("clicker")
  )
  server <- function(input, output, session) {
    nameServer("clicker")
  }
  shinyApp(ui, server)  
}

app()