Leuven Leuven - 5 months ago 70
Javascript Question

Selecting combinations of classes

I want to load divs with different combinations of classes from an external file using jquery's load function and I have some trouble grouping them correctly.

$("#somediv").load("somefile.html .class1"); // loads all divs with class1
$("#somediv").load("somefile.html .class1.class2"); // loads all divs with class1 and class2
$("#somediv").load("somefile.html .class1,.class2"); // loads all divs with class1 and all divs with class2
$("#somediv").load("somefile.html .class1.class2,.class3"); // loads all divs with class1 and class2 and all divs with class3
$("#somediv").load("somefile.html .class1,.class2.class3"); // loads all divs with class1 and all divs with class2 and class3


I would like to know how to load divs with (class1 or class2) AND class3. Something like:

$("#somediv").load("somefile.html {.class1,.class2}.class3");


I have tried grouping with (),[] and {} but none of it seemed to work.

My external file would look something like this:

<div class="class1">not selected</div>
<div class="class1 class2">not selected</div>
<div class="class2 class3">selected</div>
<div class="class1 class2 class3">selected</div>
<div class="class1 class3">selected</div>
<div class="class3">not selected</div>
...etc


Basically any combination of classes would be possible.

Answer
$("#somediv").load("somefile.html .class1.class3,.class2.class3");

I think this is what you are looking for. Will take anything that has class 3 and either class1 or class2

Comments