Uncaught TypeError: Cannot read property 'firstElementChild' of undefined when expanding posts in a certain order
<p>Can any of you guys help me out with a Javascript problem? I'm trying to write a post expansion feature for my site, and for some reason whenever I expand the first post, the second will no longer expand, and will instead redirect to the full post on the thread page. If I expand the second post and then the first next, I don't have the problem.</p>

<p>Here's the code that runs when the page is loaded:</p>

<pre><code>function postExpansionPrep(){
    if(!document.body.className){
        var links = document.getElementsByClassName("abbrev");
        for (var i = 0; i &lt; links.length; i++ ){
            (function(e) {
                links[e].firstElementChild.addEventListener("click", function(a){ expandPost(links[e].firstElementChild); a.preventDefault(); }, true);
                console.log(links[e].firstElementChild.href);
            })(i);
        }
    }
}
</code></pre>

<p>And here's the code that runs when you click on the link to expand a post.</p>

<pre><code>function expandPost(link){
    if(link.hash){
        $.get(link, function(data) {
            var loadedPost = $(data).find("#reply" + link.hash.replace("#",""));
            document.getElementById("reply" + link.hash.replace("#","")).lastElementChild.innerHTML = $(loadedPost).find("blockquote").last().html();
        });
    }
    else{
        $.get(link, function(data) {
            var loadedPost = $(data).find("#parent" + link.pathname.substring(link.pathname.lastIndexOf("/")+1));
            document.getElementById("parent" + link.pathname.substring(link.pathname.lastIndexOf("/")+1)).lastElementChild.innerHTML = $(loadedPost).find("blockquote").last().html();
        });
    }
}
</code></pre>

<p>You can see the issue in action here: <a href="http://www.glauchan.org/test/" rel="nofollow">http://www.glauchan.org/test/</a></p>

<p>By the way, the feature is opt-in, so you need to enable Post Expansion in the Board Options menu.</p>
