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

172
Views
How do I Prevent Arrow Key Functionality with JavaScript for one element and not the entire program?

Hello I'm working on developing a Data grid within a Blazor Server application. I'm hoping to avoid as much JavaScript as possible (because I'm new to it), but I believe I need JavaScript for this. My datagrid is full of focusable cells(inputs), and the content is being virtualized. I'm needing to stop the scroll functionality with arrow keys on the Datagrid Component. A temporary solution I found was to place this:

window.addEventListener("keydown", function(e) {
    if(["Space","ArrowUp","ArrowDown","ArrowLeft","ArrowRight"].indexOf(e.code) > -1) {
        e.preventDefault();
    }
}, false);

inside of the _Host.cshtml page. It works to prevent scrolling with arrow keys.

Here's the rub: I need to only prevent the arrow key event on the one Data grid Component rather than the whole window, because I use arrow keys elsewhere in the application. How can I achieve this?

EDIT

I've tried to reference the grid directly by Id to no avail. Again this code is in the _Host.cshtml and the user navigates through several pages before coming to the Datagrid. There is only one element on the datagrid containing the Id "CustomDataGrid".

var tar = document.GetElementById('CustomDataGrid');
tar.addEventListener("keydown", function (e) {
    if (["Space", "ArrowUp", "ArrowDown", "ArrowLeft", "ArrowRight"].indexOf(e.code) > -1) {
        e.preventDefault();
    }
}, false);
about 4 years ago · Juan Pablo Isaza
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!