Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

Welcome to Software Development on Codidact!

Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.

How to uncollapse the first and second tiers of a link tree in JavaScript?

+0
−1

I wish to display the first and second branches of a link tree with JavaScript.

I want to show these branches in a single action, instead of clicking each vertical arrow (link) anew.

As can be read in the linked webpage, one can click the vertical arrow to get the next branch (if there is one).

Code which I have tried and failed

document.querySelectorAll(".CategoryTreeToggle").forEach( (element)=>{
    if (element.hasAttribute('data-ct-state', 'collapsed') ) {
        element.click();
    }
});

For some reason, with the following code, all branches, whether collapsed or manually expanded, will collapse.

How to uncollapse the first and second branches of a link tree in JavaScript?

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

1 comment thread

The markup (3 comments)

2 answers

You are accessing this answer with a direct link, so it's being shown above all other answers regardless of its score. You can return to the normal view.

+1
−0

According to the documentation, the hasAttribute method expects only one argument (the attribute's name), and it tells only if that attribute is present, regardless of its value. Hence, the return is a boolean (only true or false).

When you pass more than one argument, only the first one is considered, and all the others are ignored.

In your case, all the elements have the data-ct-state attribute, therefore hasAttribute always returns true.

If you want to check an attribute's value, you can do like the other answer said (use getAttribute and check its value), but in this case you could also search for elements that have a specific value for the attribute, using an attribute selector:

document.querySelectorAll('.CategoryTreeToggle[data-ct-state="collapsed"]').forEach(e => e.click());

In the code above, querySelectorAll will search only for the elements that has the CategoryTreeToggle class and the data-ct-state attribute has the value equals to collapsed. With that, there's no need to use an if clause, because now we're sure that all the elements are the ones that I need to click.

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

0 comment threads

+4
−0

The first item your selector returns is the top level arrow. hasAttribute just tells us if the attribute is present, not what the value is. So basically your condition is returning true for all elements. Since that includes the top level element, it gets clicked and everything collapses.

Try:

document.querySelectorAll(".CategoryTreeToggle").forEach( (element)=>{
    if (element.getAttribute('data-ct-state') === 'collapsed' ) {
        element.click();
    }
});
History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.

1 comment thread

I have published a second question which you might want to comment on. https://software.codidact.c... (1 comment)

Sign up to answer this question »