javascriptjqueryjquery-selectorsjquery-attributes

Is it possible to get all elements which have a specific value into any type of attribute?


I want to get all elements in an HTML Dom, which have any attribute with the a specific value.

For Example -

<ul style="width:150px;" id="RadioButton1">
   <li style="width:150px;"><input type="radio" name="RadioButton1"><label>Option 1</label></li>
   <li style="width:150px;"><input type="radio" name="RadioButton1"><label>Option 2</label></li>    
   <li style="width:150px;"><input type="radio" name="RadioButton1"><label>Option 3</label> </li>
</ul>

I want to get all elements which contains "RadioButton1" in any attribute. Means i want to select UL and all three RadioButtons.

Note: Above HTML i used UL-LI structure, but the real scenario it's too much different. So please don't give answer in completely favor of UL-LI. I want global solution.

There are any selector exist to select all type of attribute?

Is it possible to select elements with in this approach?

Bunch of Thanks in advance...!!!


Solution

  • It can be done by filtering each element and checking its attributes:

    Fiddle

    var elements = $('*').filter(function () {
        return $(this.attributes).filter(function(){
            return this.nodeValue == 'RadioButton1'; 
        }).length;
    });