Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

How to do click and move event?

I have to draw a line in a table with my mouse when I click and hover over a <td> I tried the code below as a test but I can't make it work, the other problem is it sometimes shows the <table> element itself instead of <td>, should I use css to make the width and height of the table completely dependent of its tds without any gaps? How is it usually done?

table.addEventListener('mousedown', (e) => {
        isMouseDown = true;
    table.addEventListener('mousemove', (e) => {
                if(isMouseDown) {
        console.log(e.target.innerText);
                }
    });
});
table.addEventListener('mouseup', (e) => {
         isMouseDown = false;
});

1 Answer

Steven Parker
Steven Parker
229,982 Points

In CSS, set the table's border-spacing: 0 to remove clickable parts of the table that are between the td's.

And I'm curious .. how do you "draw a line" in the table?