Login Register

Is there a way to get multiple query items into dojo.query?

I want to select a node that has two classes in it. Like dojo.query(".dndItemOver, .dndItemAnchor") is this possible or is it only one arg? Tried a couple ways and can't find any examples on multiple queries except in stores.

Does simple CSS selector

Does simple CSS selector ".classA.classB" work for you?

You can do it exactly as you

You can do it exactly as you wrote it:

dojo.query(".dndItemOver, .dndItemAnchor")

Returns the joined search results of each query, as per the CSS 3 selector spec.

--
Project Lead, The Dojo Toolkit
President, The Dojo Foundation

I think he wanted to select

I think he wanted to select nodes that are marked by two classes at the same time. An AND selector instead of an OR selector.

That's what the comma

That's what the comma operator does. It lets you run multiple queries and get the results of both in AND fashion.

--
Project Lead, The Dojo Toolkit
President, The Dojo Foundation

Actually, I think he means...

..."get all nodes with both classes in it", i.e. <div class="dndItemOver dndItemAnchor">, as opposed to a union of nodes. IIRC, the solution Eugene suggested should work:

var nodelist=dojo.query(".dndItemOver.dndItemAnchor");

...though I haven't tested it.

Otherwise, you can always use filter to modify:

var nodelist=dojo.query(".dndItemOver").filter('return (item.className.indexOf("dndItemAnchor")>-1;)');

That's exactly what I wanted to do

That's exactly what I wanted to do. I think I'll have to use the .filter method. I believe chaining CSS selectors like this (.dndItemOver.dndItemAnchor) only works for FF and I need it to work in IE6/7 and FF.

Thanks a lot for the help. That not only solves this but a couple other issues too :)

dojo.query() is here to

dojo.query() is here to eliminate browser difference, not to enhance them. I think chaining selectors will work in any browser supported by Dojo, including IE6/7 regardless of their implementation of CSS.

thanks ttrenka

You and eugene were right. I was being dense ;-)

It's worth noting that you can also .filter() with a simple CSS selector in dojo.query():

var nodelist = dojo.query(".dndItemOver").filter(".dndItemAnchor");
--
Project Lead, The Dojo Toolkit
President, The Dojo Foundation

ooh, that's sexy.

Didn't know you'd modded it to be just an additional selector, that's sweet.