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 8537893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T11:00:42+00:00 2026-06-11T11:00:42+00:00

First time I ask something here, I hope you people will be as helpful

  • 0

First time I ask something here, I hope you people will be as helpful as ever.

I am making a script in Javascript(using some jQuery as well) that’s supposed to do this:
Suppose we have a page with user posts. If someone clicks on a user’s name, on one of the posts, the script will highlight every other post of the same user.
If the posts of a user are already highlighted, and we click on another user’s name, in another post, then all the previously highlighted posts must return to normal state, and now the posts of the other user must be highlighted instead.

The script I have written is this.

$(function (){
    $(".showall").click(function() {
        var identifier = this.innerHTML;

        var already = document.getElementsByClassName("highlight");
        var l = already.length;

        if (l) {            
            for (i=0; i<l; i++) {
                var to_hide = already[i].id;
                $("#"+to_hide).toggleClass("highlight");
            }
        }

        var sum = document.getElementsByClassName("uid_"+identifier)
        var l = sum.length;
        for (i=0; i<l; i++) {
            var to_show = sum[i].parentNode.parentNode.parentNode.id;
            $("#"+to_show).toggleClass("highlight");
        }

    });

});

Let me explain a little what it does.

When the click event is triggered on one of the specified elements, it stores the user identifier, and then checks to see if there are already highlighted comments, by checking the document for the “highlight” class.

If there are highlighted comments, it’s SUPPOSED TO remove the highlight class, from each and every one of them, and then, based on the user identifier, to highlight any other posts accordingly.

The problem with the script occurs in the loop inside the if(l) {} conditional, here

if (l) {            
    for (i=0; i<l; i++) {
        var to_hide = already[i].id;            
        $("#"+to_hide).toggleClass("highlight");
    }
}

When removing the highlight class, it does so by skipping every other post, and only does the loop for half the length.

For example, say I highlighted a user’s posts. That user had 14 posts, so, all 14 of them get highlighted. Now, if I want to highlight the posts of another user, and click on his name, the script will remove the highlight from the previous posts like this:

Remove highlight from post No1
Remove highlight from post No3
Remove highlight from post No5
Remove highlight from post No7
Remove highlight from post No9
Remove highlight from post No11
Remove highlight from post No13

When I will click on another username again, it will
Remove highlight from post No2
Remove highlight from post No6
Remove highlight from post No10
Remove highlight from post No14

etc.

I do not understand why the loop behaves that way.
I have removed the line with the .toggleClass and made it so that it alerts for every element of the array, and it did so for the whole length, not half the length, without skipping elements.

It’s like when I use the .toggleClass the i gets incremented by 2, and I, for the love of me, cannot understand why. I have also tried to do it with .classList.toggle, without using jQuery, and it’s the same thing again.

Any help will be much appreciated.
Regards to you all.

EDIT: Forgot to add, that Chrome’s console gives me this error, for the posts that the script is skipping:
Uncaught TypeError: Cannot read property ‘id’ of undefined.
I understand what it means, it’s like it did not find a valid object to read the id property, but I do not understand why it happens.

  • 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-11T11:00:43+00:00Added an answer on June 11, 2026 at 11:00 am

    Your code seems overly complex.

    Would something like this work for your use case?

    $('.user-name').click(function (e) {
        e.preventDefault();
        var user_id = $(this).parent().data('userid');
    
        if (!$(this).parent().hasClass('selected')) {    
            $('.post.selected').removeClass('selected');
            $('.post[data-userId='+ user_id +']').addClass('selected');
        } else {
            $('.post.selected').removeClass('selected');
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sorry, this's my first time to ask a question here. So, I don't have
This is my first time on this site. I will ask you to help
first time posting here. I'm using a loop to create 3 buttons, but my
first time to ask a question here. Tried to do homework already by searching
I am using some of these tools for the first time. I have read
This is my first time ever using sockets, and I am having trouble accepting
This is the first time I ask something, so if there is something wrong
First time I am using the persist() method in a project. With JPA I
first time post here. I was hoping someone could help me make custom SPARQL
First time working with JSON in Rails and trying to figure out something which

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.