0

I have a generic jQuery selector (a string):

var selector = '#files li div';

and the name of a class:

var myClass = 'folder';

I want to check if the selector matches elements having the class stored into myClass. As far as now I used an instance of the selector to invoke the method hasClass():

var match = $(selector).hasClass(myClass);

It works, but I thought it may exist some more efficient jQuery method which allows not to create a jQuery instance to get all the elements matched by the selector.
My worry is the memory used by the instance created only for this check and nothing more, althought it is inside the method of a pseudo-class which does not return/expose any reference to this instance.

Can you suggest something better?

<EDIT>
In the example showed I set the value of myClass to "folder" just to easily represent the problem, but in the app I do not know it in advance, since it is defined by user actions.
</EDIT>

BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356
yodabar
  • 4,651
  • 2
  • 33
  • 38
  • 1
    Unless I'm misunderstanding the problem, you can just use #files li div.folder – Corbin Sep 09 '11 at 10:45
  • Just check for `$('#files li div .folder').length` – m90 Sep 09 '11 at 10:46
  • @m90: It'd be `$('#files li div.folder').length` – Matt Sep 09 '11 at 10:51
  • Unfortunately no, the variable myClass can contain any value and I do not know it in advance, it is defined by user actions through the UI. In the example I set it to 'folder' just to better expose the question. – yodabar Sep 09 '11 at 11:28
  • You can use a variable in the selector as well. Just concatenate the string as you need it. – m90 Sep 09 '11 at 11:45

2 Answers2

3

hasClass() will return true if any of the matched elements has the specified class; not if all of them do.

You might find the following is more what you want:

var match = $(selector).filter('.' + myClass);

But you could then simplify it to:

var match = $(selector + '.' + myClass);

And you'd then check whether you have any elements left using the .length attribute.

if (match.length) // then some were matched!

In terms of worrying about memory usage... don't. premature optimization is the root of all evil. The memory usage here is tiny. If you want to optimize your program (is there a need at the moment?), look at functions which are called a large number of times, or look within loops that have a large number of iterations, or look at DOM manipulation and see if they can be combined; one jQuery selector isn't going to make any difference to your program's speed in relative terms.

Matt
  • 74,352
  • 26
  • 153
  • 180
  • Searching if the selection contains **almost one** element matching that class is just what I needed, that's why I used hasClass(). So I probably think the way I used is the right one. Thanks! – yodabar Sep 09 '11 at 11:35
0

Either assign Id for uniqueness or create a class if you want to apply on more than one instance

Amritpal Singh
  • 1,765
  • 1
  • 13
  • 20