Get child by attribute value

I have a jQuery object representing a DIV:

$rel = $('#rel_'+rel.id); 

There is a BUTTON in this DIV with my custom "rid" attribute set to rel.id

I need to select this button, it works as follows:

 $("[rid='"+rel.id+"']").html(); 

But I think this is not the fastest solution, because for this you need to parse the entire DOM, and since I know that the button is always inside the DIV, I could avoid this.

I tried it with the following code:

 $rel.children("[rid='"+rel.id+"']").html(); 

but it didn’t work.

Thanks for any help.

+6
source share
1 answer

If he cannot be an immediate child, you will need to find , not children (which looks only at immediate children):

 $rel.find("[rid='"+rel.id+"']").html(); 
+14
source

All Articles