Okay, I had to re-word this to make better sense of what I was asking.
So I have a block filled with divs, and I want to cause them to change color if clicked. So, if I use an inline reference with this the function works properly.
<html><head><style>
body {font-family:"BACKTO1982", System, san serif; background-color:#666; font-weight: normal; font-size: normal; padding: 0; margin: 0; height: 100%; min-height: 700px; width: 100%; min-width: 800px; overflow: hidden;}
#theBlock {position:relative; height: 640px; width: 800px; margin:20px auto 20px auto; padding:0; overflow:hidden;}
.blocks {height:12px;width:12px;border:1px solid #eee;background-color:#ffffff;margin:0;padding:0;display:inline-block;}
</style><script>
x = 0;
window.onload = function(){
for(i=0;i<3200;i++){
var newBlock = document.createElement('div');
newBlock.setAttribute('class', 'blocks');
newBlock.setAttribute('id','blockId'+x);
newBlock.setAttribute('onclick','change(this);');
document.getElementById('theBlock').appendChild(newBlock);
x++;
}
}
function change(x){
x.style.backgroundColor = '#000000';
}
</script></head><body><div id="theBlock"></div></body></html>
However as inline Javascript is not the cleanest way to code, so I attempted to make a function using a this reference, in place of change() like so:
document.getElementsByClassName('blocks').onclick = function(){
this.style.backgroundColor = "#000000";
}
But this function seems to not really do anything. I am not sure if this is a matter of it not properly referencing the object, or is malformed, but my console doesn't seem to find any issue with it, it just doesn't work.
Hopefully this makes more sense than my first attempt at this question.