Skip to content Skip to sidebar Skip to footer

Jquery: Select An Element When Two Or More Conditions Are True

I have an element like this
  • li and class myclass would be:

    li.myclass

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

    li[data-id=45][data-ud=322]

    or, with jquery:

    $("li[data-id=45][data-ud=322]")
    

    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:

    $("li").filter("[data-id=45]").filter("[data-ud=322]")
    

    Fiddle example: https://jsfiddle.net/o9fdhphf/

Solution 2:

Try the following:

$('li[data-id=45][data-ud=322 ]');

Solution 3:

$("[data-ud=322][data-id=45]").text()

OR

$("[data-ud=321] + [data-id=45]").text()

JSFiddle

Post a Comment for "Jquery: Select An Element When Two Or More Conditions Are True"