Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8361343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T11:40:32+00:00 2026-06-09T11:40:32+00:00

Can any of you guys help me out with a Javascript problem? I’m trying

  • 0

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.

Here’s the code that runs when the page is loaded:

function postExpansionPrep(){
    if(!document.body.className){
        var links = document.getElementsByClassName("abbrev");
        for (var i = 0; i < 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);
        }
    }
}

And here’s the code that runs when you click on the link to expand a post.

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();
        });
    }
}

You can see the issue in action here: http://www.glauchan.org/test/

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T11:40:33+00:00Added an answer on June 9, 2026 at 11:40 am

    The document.getElementsByClassName method returns a live node list. Therefore, the links will change, and potentially have not item at position e any more when the handler is fired – resulting in an undefined value that throws an error when accessing its firstElementChild property.

    You should not need to get the current element from the links list. Get it dynamically via a.target or just this:

    function postExpansionPrep() {
        if (document.body.className)
            return;
        var links = document.getElementsByClassName("abbrev");
        for (var i = 0; i < links.length; i++ ) {
            var child = links[i].firstElementChild;
            if (!child) // could be null as well
                continue;
            child.addEventListener("click", function(e) {
                expandPost(this);
                e.preventDefault();
            }, true);
            console.log(child.href);
        }
    }
    

    Btw, as you have jQuery available you should use it, especially for the event handler attaching:

    if (!document.body.className)
        $(".abbrev > :first-child").click(function(e) {
            expandPost(this);
            e.preventDefault();
        });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a problem so hope you guys can help! I have a string
Hope you guys can help me out. I have information coming into a serial
Okay guys, maybe you can help me out with this one. I'm about ready
I hope you guys can help me out. I'm using C# .Net 4.0 I
Wondering if you can help me out. I seem to have a problem changing
maybe you guys can help me figure this out: I have a Dictionary and
Can any one explain difference between position and anchor point in cocos-2D with some
Can any expert here please point me to some valid walkthrough on how to
Can any one help in transforming my xml though XSLT. I am new to
I'm not trying to ask you guys to help me to do homework because

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.