Most efficient way to solve an adjacent element problem in vanilla js + html?

Most efficient way to solve an adjacent element problem in vanilla js + html?

This is the problem:

This is the problem:

Create a 10x10 grid using html, vanilla javascript, and css.

When a square on that grid is clicked, change the background color of the adjacent squares (the one on top, to the left, etc) to yellow.

I was able to solve this problem, but I don't think it's efficient - My html is only one line:

<table id="grid"></table>

And I used javascript to manually populate the table with a bunch of cells/squares. I gave each cell an two attributes data-row and data-col to store where the cell is located. Then on click I'm doing something like this:

  var row = this.getAttribute('data-row');
  var col = this.getAttribute('data-col');

...

//if not on the top most row, retrieve square above if(row>0){ let sel = "td[data-row='"+(row-1)+"'][data-col='"+col+"']"; let elem = document.querySelector(sel); elem.classList.toggle('yellow'); }

...

//3 more if blocks like the one above to find the bottom, left and right squares

Is there a more efficient or concise way of doing this? Thank you for your responses in advance! It's appreciated :)

Angular 9 Tutorial: Learn to Build a CRUD Angular App Quickly

What's new in Bootstrap 5 and when Bootstrap 5 release date?

Brave, Chrome, Firefox, Opera or Edge: Which is Better and Faster?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

How to do basic Animations with HTML5 Canvas Element and JavaScript API

In this Canvas crash course, you'll learn how to do basic animations with HTML5 Canvas Element and JavaScript API. We will look at the HTML5 Canvas element and JavaScript API to draw shapes, paths and do some basic animation. <canvas> is an HTML element that allows a user to create graphics using JavaScript. To start a canvas project, we will need an HTML file and a Javascript file. Using JavaScript to control <canvas> elements, it's also very easy to make (interactive) animations

Building a Simple URL Shortener With HTML and Javascript

In this article, you''l learn how to build a simple URL shortener with HTML and Javascript