Skip to content
Advertisement

Error or bug when switching CSS classes by JavaScript when dragging with mouse [closed]

Ok, here a simple code, which changes td class when you click on them:

JavaScript
JavaScript
JavaScript

If you click it — anything works fine, but now try to click on cell number one and drag mouse over cell number three and then release mouse button — class will be assigned to tr not to last td number 3.

Why is that?

https://jsfiddle.net/foxnadir/Ls6p7j1z/3/

Advertisement

Answer

The problem in your code was that you were attaching the click event to the table element, when only one td can be clicked at a time.

I also changed the event from click to mousedown so that when the user drags from 1 to 3, 1 changes color, but if this behavior is not what you wanted, you can change it back.

Here is the working code:

JavaScript
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement