Tuesday, May 29, 2012

jQuery multiple class selector


I want to select all the elements that have these two classes 'a' and 'b'. So, only the elements that have both classes. When I use $(".a, .b") it gives me union, but I want intersection.



Source: Tips4all

2 comments:

  1. This should work:

    $('.a.b')


    If you want an intersection, just write the selectors together without spaces in between. So for something that has ID 'a' and classes 'b' and 'c', you'd do:

    $('#a.b.c')

    ReplyDelete
  2. You can do this using the filter function:

    $(".a").filter(".b")

    ReplyDelete