I am trying to modify the innerHTML of a div when clicked. I have dynamically generated an array of divs, and I want to specifically access the table which the user has clicked.
I have tried dynamically setting the ID of the tables in the JavaScript code:
(for var i=0;i<array.length;i++){
var IDvalue = "foo";
// append table to HTML string.
html += "<div onclick='modifyInnerHTML(\"" + IDvalue + "\")'>...</div>";
var divs = document.getElementsByTagName('div');
divs[divs.length-1].id=array[i].IDvalue;
}
Then in modifyInnerHTML() I access the element by ID:
function modifyInnerHTML(id){
document.getElementById(id).innerHTML+="Add an update";
}
While I think this code should work fine, it is breaking at the document.getElementById(id) function, and I realize the id of the divs is not being set dynamically. Any thoughts on why this might be or are there faster ways to do this?
Thanks in advance!
divs[divs.length-1].id=array[i].IDvalue;This seems wrong to me.array[i].IDvaluesyntax works.