I am trying to change the color of the elements, changing their class, while the mouse is held down, giving the effect of "painting". Doing it by mouse clicks I have it, here I leave the code:
$(() => {
$('table').on("click", "td", (event)=> {
let selected = $(event.target);
selected.toggleClass("obstacle");
});
})
table {
border-collapse: collapse;
}
td {
padding: 20px;
border: 2px solid black;
cursor: pointer;
}
.obstacle {
background-color: red !important;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</table>
Do you know any way to change the style when you click on the mouse? Without having to make as many clicks as you want changes.
I've tried with events like OnMouseDown and so on, but I'm not able to find the mode.
The idea is while you have the mouse pressed, do something like this:
Greetings and thanks in advance