Find element that has either class 1 or class 2

Gabriele Petrioli

If you want the first one found (but only one) use

var myText = $(this).find('.myClass1,.myClass2').eq(0).text();

If you want the first of each kind (two results) then look at the answer provided by @jelbourn.

You can separate your selectors with commas to generate a list containing all elements with either class (or with both):

var elements = $(this).find('.myclass1:first, .myclass2:first');

Enter a comma between the two classes in your selector.

$(".a, .b")

this will match all elements with class "a" OR class "b"

Use an if statement and the jQuery hasClass() function:

It would probably look something like this:

if($(this).hasClass('myClass1') || $(this).hasClass('myClass2')) {
  myText = $(this).text();
} else {
  myText = null;