Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript. get all element by aria-label

Tags:

javascript

Hey im trying to get every single element on a page that has an aria label that contains/starts with "i_". I also need to do this in javascript.

thanks

like image 703
RetroPanda Avatar asked Apr 13 '26 17:04

RetroPanda


1 Answers

In CSS, you would use the [attribute^=value] format:

[aria-label^="i_"] {
    background: #55ff55;
}

In JavaScript, just use the same thing:

document.querySelectorAll("[aria-label^='i_']");

That should return an array-like list of all the elements with aria-label values that start with i_.

Here's an example with both of them:

var x = document.querySelectorAll("[aria-label^='i_']");
// 'x' contains all of the <p> elements that have "i_" in the start
[aria-label^="i_"] {
    background: #55ff55;
}
<p aria-label="i_foo"> Giraffes have black tongues to protect them from getting sunburnt </p>
<p aria-label="i_bar"> Coral colonies can live up to centuries old </p>
<p aria-label="m_foo"> The moon is a planet </p>
<p aria-label="i_gaw"> Ostriches' eyes are larger than their brains </p>
like image 192
Matheus Avellar Avatar answered Apr 15 '26 09:04

Matheus Avellar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!