I didn't find a way to get aria-expanded value from DOM.
<a class="accordion-toggle collapsed" href="#collapse-One" data-parent="#accordion-1" data-toggle="collapse" aria-expanded="false"> <i class="fa fa-search-plus"></i> test </a>
I want to test if it's true
then I can change <i>
class to fa-search-minus
. I tried this but I always get an undefined value:
console.log($(this).find('a.aria-expanded').val());
Another way to get aria values is to reference the properties directly: For your instance, you could do something like this: let firstAnchorTag = document. getElementsByTagName('a')[0]; // I don't know your situation but I recommend adding an id to this element, or making this an iterable array.
The aria-expanded attribute is simply a flag for the user agent. It. Indicates whether the element, or another grouping element it controls, is currently expanded or collapsed. ... where that indication is for the element's contents, or if aria-controls is also specified, for the target element.
To use aria-expanded="true" to change a CSS property, we can use the aria-expanded="true" selector. to add 2 links. to select the a elements with the aria-expanded attribute set to true and set their background color to #42dca3 .
Combobox. By default, some roles are hidden or collapsed and other roles are open or expanded by default. Elements with role combobox have a default value for aria-expanded of false . When a combobox popup is not visible, the element with role combobox has aria-expanded set to false .
aria-expanded
is an attribute on the element, not a class, so the selector is incorrect. Secondly, you should use the attr()
function to get the value of that attribute. val()
is intended to retrieve the value
attribute from form related elements, such as input
and textarea
. Try this:
console.log($(this).find('a[aria-expanded]').attr('aria-expanded'));
I wanted to add in a second pure javascript way to get the aria-expanded attribute
document.getElementById('test').getAttribute('aria-expanded')
The above will get the element by id after that you can get any attribute by name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With