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.
Post History
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 ...
Answer
#1: Initial revision
According to the [documentation](https://developer.mozilla.org/en-US/docs/Web/API/Element/hasAttribute), 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](https://software.codidact.com/posts/286216/286217#answer-286217) (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](https://developer.mozilla.org/en-US/docs/Web/CSS/Attribute_selectors): ```javascript 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.