Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

JavaScript ARIA Role equivalent of nodeName

If I want to get the nodeName for all HTML ins elements I can use

nodes =       document.querySelectorAll("ins");
        for (let n = 0; n < nodes.length; n++) {
          nodes[n].setAttribute("data-name-prohibited", nodes[n].nodeName);
        }

This returns data-name-prohibited="ins”

I want to get the ARIA Role type

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

nodes = document.querySelectorAll("[role=insertion]");
        for (let n = 0; n < nodes.length; n++) {
          nodes[n].setAttribute("data-name-prohibited", nodes[n].roleName);
        }

roleName is just an example. What I want is data-name-prohibited="insertion"

>Solution :

You can get the role attribute value and pass that to the data-name-prohibited attribute.

const nodes = document.querySelectorAll("[role=insertion]");
for (let n = 0; n < nodes.length; n++) {
  const currentNode = nodes[n];
  const role = currentNode.getAttribute('role');
  currentNode.setAttribute("data-name-prohibited", role);
}

But if you use the [role=insertion] query, then you already know the role value, so you could simply hardcode it.

const role = 'insertion';
const nodes = document.querySelectorAll(`[role=${role}]`);
for (let n = 0; n < nodes.length; n++) {
  nodes[n].setAttribute("data-name-prohibited", role);
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading