We're working on a filter system, where you get groups of options, for instance:
Colour Red, Blue, Green
Price Range £0-£5, £6-£10, £11+
Make Adidas, Nike, Converse
Each option is a single checkbox. We filter with the following logic:
So, if I was to check Red, Blue, £0-£5, Nike and Converse, the logic would be:
(Red OR Blue) AND (£0-5) AND (Nike OR Converse)
Each checkbox has an ID. I wish to build a selector using the logic above, but I'm unsure about the best way to do it in jQuery. Obviously ANDs are just appended and ORs (or the equivalent) use commands in these selectors, but that would break the logic without some way of grouping, i.e.:
$('(#red,#blue)(#price0to5)(#nike,#converse)')
Any suggestions or alternatives?
ID and Element selector are the fastest selectors in jQuery.
You can specify any number of selectors to combine into a single result. Here order of the DOM elements in the jQuery object aren't necessarily identical.
You can specify any number of selectors to combine into a single result. This multiple expression combinator is an efficient way to select disparate elements. The order of the DOM elements in the returned jQuery object may not be identical, as they will be in document order.
The jQuery #id selector uses the id attribute of an HTML tag to find the specific element. An id should be unique within a page, so you should use the #id selector when you want to find a single, unique element.
Let's assume you have a bunch of items with the applicable class identities:
<div class="red nike underfive"></div>
<div class="blue green converse elevenup"></div>
<div class="blue converse underfive"></div>
ORs are equivalent to commas, while ANDs require chaining:
$(".red,.blue").filter(".underfive").filter(".nike,.converse"); // 1 and 3
$(".green").filter(".underfive"); // empty
$(".blue").filter(".green"); // 2
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With