Do not you think it would be better to manage it with CSS?
.celdadibujo {
cursor: auto;
}
.celdadibujo:hover {
cursor: pointer;
}
That would work so that when you move the mouse over, the cursor changes. But you also want to suppress that behavior on certain occasions.
Then let's say that the behavior only occurs when the elements .celdadibujo
have also the class cursorpointer
.
.celdadibujo {
cursor: auto;
}
.cursorpointer:hover {
cursor: pointer;
}
So you can add the class (and therefore the hover) by invoking:
function activar() {
var celdas = document.querySelectorAll('.celdadibujo');
celdas.forEach(function (celda) {
celda.classList.add('cursorpointer');
});
}
And remove the class by invoking
function desactivar() {
var celdas = document.querySelectorAll('.celdadibujo');
celdas.forEach(function (celda) {
celda.classList.remove('cursorpointer');
});
}
The function that you put in the question assigns to each cell the id
defined by the variable color
. You can not ( do not ) have more than one node with the same id. But if you want to change the color of the cell and also make a click on one of them deactivate the color, the pointer and delete the listener on the event click
:
function desactivar() {
var celdas = document.querySelectorAll('.celdadibujo');
celdas.forEach(function (celda) {
celda.className = 'celdadibujo';
celda.removeEventListener('click', desactivar);
});
}
function activar(color) {
var celdas = document.querySelectorAll('.celdadibujo');
celdas.forEach(function (celda) {
celda.className = 'celdadibujo';
celda.classList.add('cursorpointer');
celda.classList.add(color);
celda.addEventListener("click", desactivar);
});
}
table {
border: 1px solid;
}
td {
border: 1px solid;
padding: 2px 5px;
}
.celdadibujo {
cursor: auto;
}
.cursorpointer:hover {
cursor: pointer;
}
.buttoncolor {
padding: 7px 7px;
margin: 0 3px;
vertical-align: super;
}
.cursorpointer.red {
border: 1px solid red;
}
.cursorpointer.blue {
border: 1px solid blue;
}
.cursorpointer.green {
border: 1px solid green;
}
#red {
background-color: red;
}
#blue {
background-color: blue;
}
#green {
background-color: green;
}
<button class="buttoncolor" id="red" onclick="activar(this.id)"></button>
<button class="buttoncolor" id="blue" onclick="activar(this.id)"></button>
<button class="buttoncolor" id="green" onclick="activar(this.id)"></button>
<button onclick="desactivar()">Desactivar cursor pointer</button>
<table border="1">
<tr>
<td class="celdadibujo">Texto A</td>
<td class="celdadibujo">Texto B</td>
<td class="celdadibujo">Texto C</td>
</tr>
<tr>
<td class="celdadibujo">Texto D</td>
<td class="celdadibujo">Texto E</td>
<td class="celdadibujo">Texto F</td>
</tr>
<tr>
<td class="celdadibujo">Texto G</td>
<td class="celdadibujo">Texto H</td>
<td class="celdadibujo">Texto I</td>
</tr>
</table>
In case you want to add the behavior to the whole body, the function is simpler. You just have to add and remove the class of the body element instead of iterating every .celdadibujo