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
Notifications
Mark all as read
Q&A

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?

Why does this post require moderator attention?
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

+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();
    }
});
Why does this post require moderator attention?
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)
+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.

Why does this post require moderator attention?
You might want to add some details to your flag.

0 comment threads

Sign up to answer this question »

This community is part of the Codidact network. We have other communities too — take a look!

You can also join us in chat!

Want to advertise this community? Use our templates!

Like what we're doing? Support us! Donate