We help IT Professionals succeed at work.

addEventListener on getElementsByClassName

Don't know why this doesn't work?

window.onload  = function (){
    var inner = document.getElementsByClassName('inner');
    inner[0].addEventListener("scroll", runthis);
}

Receive an error on console:

Cannot read property 'addEventListener' of undefined

The class exists and there's only one of them. So I'm only targeting the first and only element.
Comment
Watch Question

Most Valuable Expert 2013
Awarded 2012

Commented:
Hi,
what does this show:
window.onload  = function (){
     var inner = document.getElementsByClassName('inner');
     alert(inner.length);
 }

Open in new window

Says "1"

Also I tried my code locally and it works, so perhaps it has something to do with the "inner" div being loaded dynamically??
This is weird. On the page in question, the HTML is located directly in the <script> tag... what does this mean?

<script type="text/template">

                        <div class="inner">
                              <table class="items">
                                    <tbody></tbody>
                              </table>
                        </div>
</script>
Most Valuable Expert 2013
Awarded 2012
Commented:
Hi,
this is normally used to create "pure" JavaScript templating (not using any custom JS framework like AngularJS or knockout).
See:
http://en.wikipedia.org/wiki/JavaScript_templating

But without any references this does not really make sense.

HTH
Rainer