On many an occasion I've found this to simply not work for me, when I view the source of the generated page the ID will be missing, i.e. my script will read
document.getElementById(''). This has been quite a source of considerable frustration at times and the documentation here doesn't really hint at the solution; what it does do is have a link to Best Practices for Accessing Component IDs, and therein lies the magical answer.
I urge you to go and read this page, but here is the vital part:
$Componentglobal variable simplifies references and reduces some of the dependency on the overall page structure. For example, to access a data table with
id="tableID"contained in a page block with
id="blockID", use the following expression:
That's really all there is to it, you must drill down through the IDs of all the containers, mystery solved.