Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get aria-expanded value

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()); 
like image 648
Besbes Riadh Avatar asked Aug 01 '15 13:08

Besbes Riadh


People also ask

How do I expand my aria value?

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.

What is aria-expanded?

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.

How do you target aria-expanded in CSS?

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 .

Why is aria-expanded false?

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 .


2 Answers

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')); 
like image 80
Rory McCrossan Avatar answered Sep 19 '22 17:09

Rory McCrossan


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.

like image 33
Jon Avatar answered Sep 20 '22 17:09

Jon