scriptkiddie scriptkiddie - 1 year ago 68
Javascript Question

Jquery: select an element when two or more conditions are true

I have an element like this

<li data-ud="321" data-id="42">
<li data-ud="322" data-id="42">
<li data-ud="323" data-id="42">
<li data-ud="324" data-id="42">

<li data-ud="321" data-id="43">
<li data-ud="322" data-id="43">
<li data-ud="323" data-id="43">
<li data-ud="324" data-id="43">

<li data-ud="321" data-id="44">
<li data-ud="322" data-id="44">
<li data-ud="323" data-id="44">
<li data-ud="324" data-id="44">

<li data-ud="321" data-id="45">
<li data-ud="322" data-id="45">
<li data-ud="323" data-id="45">
<li data-ud="324" data-id="45">


Now I want to select a element with data-id=45 and data-ud=322. I have tried but it didn't work.

Thanks in advance

Answer Source

To use 'and' in a selector, simply don't use any other separator, eg a li and class myclass would be:


to find by attribute, use [attribute=x], so to combine the two:


or, with jquery:


This can be difficult to read and prone to errors eg a space between the two [][] will give a totally different result, so you can instead use filter to the same effect:


Fiddle example:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download