Tengo una tabla y cada celda tiene una ID única, ¿cómo acceder al valor src y cambiarlo?
<tr><td id="ROOM11"><img src="01.png"></td><td id="ROOM12"Este código JavaScript intenta acceder al src pero devuelve un valor nulo:
let cellid = document.querySelector("#ROOM11"); console.log(cellid.getAttribute('id'));//I manage to access the ID: returns ROOM11 cellid.setAttribute('src',02.png'); //returns with an error cellid.getAttribute('src'); //returns with nullTal vez necesito acceder al elemento secundario pero no sé cómo acceder a él.
Cambie su selector a #ROOM11 img . #ROOM11 por sí mismo solo seleccionará el elemento con el id ROOM11 .
let cellid = document.querySelector("#ROOM11 img"); console.log(cellid) cellid.setAttribute('src', 'https://www.gravatar.com/avatar/0fdacb141bca7fa57c392b5f03872176?s=48&d=identicon&r=PG&f=1'); <table> <tr> <td id="ROOM11"><img src="01.png"></td> </tr> </table>