How to select all elements with particular ARIA value using jQuery?

SharkLaser picture SharkLaser · May 30, 2014 · Viewed 49.2k times · Source

Given i have a sample page that looks like this:

<!DOCTYPE html>
<html>
<body>

<h1 aria-controls="name1">heading</h1>

<p aria-controls="name2">paragraph</p>

<span aria-controls="name1">span</span>

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</body>
</html>

How would i use jQuery to select the (2) elements with their aria-controls attribute set to name1? (ignoring the fact that the element types are different).

Thank you!

Answer

JAAulde picture JAAulde · May 30, 2014

The attribute selector

[aria-controls="name1"]

should work.

Docs: http://api.jquery.com/attribute-equals-selector/