Can you choose more than one table cell with just one click using JavaScript?

Recently, I used javascript to enhance the cursor icon size on a webpage and incorporated a table. Now, I am looking for a way to change the background color of all overlapping elements in the table when the cursor icon interacts with them (during click and drag actions). Essentially, I want to expand the selection area affected by mousedown or mouseover events to allow for selecting multiple elements simultaneously. Is there a method to achieve this functionality and if so, how can it be implemented?

Below is the code snippet for clicking:

$('td').on('mousedown', function (e1) {
    loop();
    flag = 1;
    $('td').on('mousemove', function (e2) {
        console.log(document.elementsFromPoint(e2.pageX, e2.pageY));
        if (flag == 1) {
            var tableCell = $(this);
            var setbox = $('.active').attr('id');
            switch (setbox) {
                case "lPark":
                    $(this).css('background-color', 'green');
                    break;
            }
        }
    });
}

And here is the code snippet for increasing cursor size:

var cursor = document.createElement('canvas'),
ctx = cursor.getContext('2d');

cursor.width = 100;
cursor.height = 100;
var centerX = cursor.width / 2;
var centerY = cursor.height / 2;
var radius = 40;

// Code continues...

To visualize my objective, you can view the image at the following link: https://i.sstatic.net/GWCnd.jpg

In summary, while the current setup allows only one cell to be selected when the cursor hovers over multiple cells, I aim to enable the selection of all intersecting cells.

Answer №1

I made an attempt to comprehend your question. There is certainly one approach you can take.

There are numerous methods to achieve this goal. Nonetheless, here's a simple algorithm that I recommend:

1) Detect the ctrl key press event and store a boolean flag value (let's call it x) in a hidden field.
  1.1) Save true in x on Key down
  1.2) Save false in x on Key up
2) While the program is running, capture mouse clicks on cells
  if( x==true)
      add a highlight class to the clicked cell
  else
      remove the highlight class from the cell
3) Now, your entire table/grid will have highlighted items for selection

Each step can be tailored to meet specific requirements. I trust this provides some insight.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to apply a mask to a textbox to format the date as MM/yyyy using a mask

In my asp.net application, I have a TextBox for entering Credit card date (month & year only). I tried using the 'TextBox with masked edit extender' and set Mask="99/9999" with Mask Type="Date. However, it is not working as expected - it only wor ...

I am looking to adjust/modulate my x-axis labels in c3 js

(I'm specifically using c3.js, but I also added d3.js tags) I have been working on creating a graph that displays data for each month based on user clicks. However, I am facing an issue where the x-axis labels show 0-X instead of 1-X. For instance, ...

Is it possible to utilize jQuery for extracting information from HTML pages?

During my project research involving parsing and extracting content from HTML pages, I stumbled upon jQuery. Can jQuery be used for this purpose? If yes, could someone provide examples or share tutorial links with me? One idea I have is to parse the top q ...

Guide to sending checkbox data using jQuery AJAX

I am facing an issue with submitting the form below: <form action="/someurl" method="post"> <input type="hidden" name="token" value="7mLw36HxPTlt4gapxLUKWOpe1GsqA0I5"> <input type="checkbox" class="mychoice" name="name" value="appl ...

Trouble with the filter function in the component array

I am facing an issue with creating and deleting multiple components. I have successfully created the components, but for some reason, I am unable to delete them when I click on the "delete" button. state = { data: '', todoCard: [], id ...

What is the process of integrating data from the OpenWeatherMap API into the WindowInfo feature of Google Maps?

My current project involves retrieving weather information from the openweathermap API and displaying it in an infowindow on Google Maps. However, there seems to be an issue where I am getting data from the previous marker instead of the current one. var ...

Recording setInterval data in the console will display each number leading up to the current count

Currently, I am developing a progress bar that updates based on a counter over time. To achieve this, I opted to utilize a setInterval function which would update the counter every second, subsequently updating the progress bar. However, I encountered an ...

The Significance of Server-Side JavaScript

How common is the use of server-side JavaScript? What are the advantages of using it over other server-side scripting languages? Can you point out specific use cases where it outperforms other options? I'm also unsure about how to start experimenting ...

The Typescript SyntaxError occurs when attempting to use an import statement outside of a module, typically within a separate file that contains

I am currently developing a Minecraft bot using the mineflayer library from GitHub. To make my code more organized and reusable, I decided to switch to TypeScript and ensure readability in my project structure (see image here: https://i.stack.imgur.com/znX ...

Exploring modifications in axis for Google charts timeline

Can anyone help me figure out how to set my Google Timeline chart to always display 24 hours on the x-axis? Currently, it automatically changes based on the earliest and latest points, but I want it to consistently show all 24 hours. For example: ...

Creating a custom Jquery easing function with an arrow that moves up and down

I am trying to implement Jquery easing with two arrows (an up arrow and a down arrow). It should look like there is a circle at the top of the box containing an up arrow and a down arrow. The idea is that when the box moves down, the up arrow appears, and ...

Developing Angular 6 for dynamic HTML navigation tabs

Being new to Angular 6, I recently created tabs in HTML and now need to convert them into Angular 6 while also making them dynamic. Here is the original HTML code: <ul class="nav nav-tabs side_nav" role="tablist"> <li role="presentation" class= ...

Issues within html language

The formatting of this table is causing issues with printing <tr height="95%"> <td width="100%"> <div id="placeSchedule"> <table bgcolor="red" width="100%" height="100%" border="1"> <tr> < ...

Are there any other commands similar to "now dev" available on Vercel?

"now dev" is not being recognized as a command on my Windows 10 system with Vercel, JavaScript, Node.js, and MongoDB. I am trying to test my API using Postman, but when I enter the "now dev" command in the command prompt, it does not wo ...

Uncover the structure of a regular expression pattern to discover the quantity of tokens and the size of the anticipated matches

I am looking to validate certain fields in my forms by determining the number of tokens and length specified by a particular regular expression. For example, For the pattern [0-9]{3},[0-9]{2}, I need to identify 5 as the length and 2 as the number of to ...

Error: Uncaught TypeError in Ajax function definition

I encountered an error in my code that says: Uncaught TypeError: Cannot read property 'toLowerCase' of undefined The interesting thing is, when I remove the following part from my source file: delay(function(){ ... }, 1000); the code works ...

The scrollIntoView function is not functioning as expected

Having an issue with my function called scrollIntoView. It just refuses to work :( Take a look at the code below: HTML <section class="page_mission"> <div class="page_mission_text"> <div class="scroll-animations"> <di ...

Event handler assigned to a group of constantly changing elements

Here is my JavaScript code: $(document).ready(function(){ $('#data1').change(function(){ title = $('#title1').val(); url = $('#url1').val(); $.post('library/edit.php',{title:title, url:ur ...

Determine the presence of a JSON Value/Array in a web application using JavaScript and visualize the information in a dynamic

Utilizing the Ticketmaster API has provided me with a sample dataset from their platform, Data 1 - Including information on "presales." "sales": { "public": { "startDateTime": "2019-11 ...

Unable to transfer the variable name between different node modules for the purpose of using it as a chat room identifier in a socket.io application

Users are able to provide a room name, however when using module.exports in a separate file to retrieve it, the value shows as undefined. This issue likely arises from the asynchronous nature of the process. //roomcheck.js var nsp = io.of("/gameroom"); n ...