How can I get jQuery $('.class', context) to include context itself if it matches .class?

Posted by Jake on Stack Overflow See other posts from Stack Overflow or by Jake
Published on 2010-03-18T20:55:43Z Indexed on 2010/03/18 21:01 UTC
Read the original article Hit count: 390

Filed under:
|

I want to be able to match against all elements in a given context including the context element itself.
Here is the code I'm using currently, but it seems inefficient. Is there a better way?
Note: I'm using jQ 1.3.2, but I'll upgrade soon so I'm interested in 1.4 solutions too.

var context = $('#id');   
var filters = '.class1, .class2';

// take context itself if it matches filters
$(context).filter(filters)
// add anything matching filters inside context
.add($(filters, context))

Note: .add($(f,c)) works in jQ 1.3 as .add(f,c) in jQ 1.4

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about jquery-selectors