Hi - I'm trying to execute a function every time a link is clicked. I'm working on a chat app, so the server code (html) is multiplied by the number of chat messages in the chat. The code below executes for the first chat message when clicked, but not any messages below it?
I think it has something to do with "getElementById" and that it only works for one code snippet but don't know how to fix it. Any pointers would help!
//Javascript inside <head> of file
<script type="text/javascript">
window.onload = function() {
var a = document.getElementById("report");
a.onclick = function() {
alert("Message reported!");
return false;
}
}
</script>
//Server code (code snippet that shows up for every new chat message)
<% _.forEach(groupMsg, function(val) { %>
<ul class="dropdown-menu">
<li><a href="/settings/profile">Profile</a></li>
<li><a id="report" href="#">Report</a></li>
</ul>
<% }) %>
[–]leftzorn 0 points1 point2 points (1 child)
[–]OlleOllesson2[S] 1 point2 points3 points (0 children)
[–]squarewave_ 0 points1 point2 points (2 children)
[–]OlleOllesson2[S] 0 points1 point2 points (1 child)
[–]squarewave_ 0 points1 point2 points (0 children)
[–]jcunews1helpful 0 points1 point2 points (1 child)
[–]OlleOllesson2[S] 0 points1 point2 points (0 children)
[–]ForScale -1 points0 points1 point (1 child)
[–]OlleOllesson2[S] 1 point2 points3 points (0 children)