How to Apply onclick funtions to several elements of the same class with JS
Hey guys, how can I aplly a onclick function to several elements of the same class or ID's? I want to change the image (src) when I click the element with the class status. For example: HTML: ........ <tr> <td>Reunião de kickoff</td> <td><a href="guides/kickoff.pdf">Link</a></td> <td>Flexy</td> <td><img class="status" src="images/toDo.png"/></td> </tr> <tr> <td>Entrega dos acessos ao painel administrativo</td> <td></td> <td>Flexy</td> <td><img class="status" src="images/toDo.png"/></td> </tr> <tr> <td>Definição do Gateway de pagamentos</td> <td></td> <td>Cliente</td> <td><img class="status" src="images/toDo.png"/></td> </tr> ........ JS: window.onload = function () { var task = document.getElementsByClassName("status"); var statusToDo = "images/toDo.png"; var statusDone = "images/done.png"; for (i = 0; i < task.length; i++) { task.onclick = function () { if(task.src == statusToDo) { ta sk.src = statusDone; } else { task.src = statusToDo; } } } } Can anyone help me? PS: I don't want to put the onclick funtion on the element inside the HTML file, instead I want it to be done by the JS.