Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

283
Views
Vue keydown event not firing on div

As the title says, I can't get my event to fire.

@keydown="keyPressed($event)"

This is how it looks currently,but I've tried it with tabIndex and appending in on mount like this :

mounted(){
        let container=document.querySelector('#course')
        container.addEventListener('keydown',(e)=>{
            console.log(e)
        })

    }

Template

<div id="course" tabindex="0" @keydown="keyPressed($event)"> 
  <div class="snake"></div>
</div>

Hence,nothing works. What am I doing wrong?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

I think in vue, a better practice would be to select the element using ref instead of a querySelector especially if it can be referenced in the same component.

Also using @keydown just attaches a keydown event listener to the element, so there is no need to do addEventListener.

Working example in vue

<script setup>
  import { onMounted } from 'vue';
  
  const courseDiv = $ref(null);
  
  const keyPressed = (e) => {
    console.log(e);
  }

</script>

<template>
  <div ref="courseDiv" id="course" tabindex="0" @keydown="keyPressed($event)"> 
    <div class="snake">snek</div>
  </div>
</template>
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!