Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get child of element by attribute value

I have a jQuery object representing a DIV:

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

In that DIV, there is a BUTTON with my custom attribute "rid" set to rel.id

I need to select that button, it works like this:

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

But I think that's not the fastest possible solution as it needs to parse the whole DOM for that and because I know the button is always inside the DIV, I could avoid that.

I tried it with the following code:

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

but that didn't work.

Thanks for any help.

like image 980
Raphael Jeger Avatar asked Apr 20 '13 07:04

Raphael Jeger


1 Answers

If it may not be an immediate child, you'll want find rather than children (which only looks at immediate children):

$rel.find("[rid='"+rel.id+"']").html();
like image 195
T.J. Crowder Avatar answered Nov 15 '22 02:11

T.J. Crowder