user2060802 user2060802 - 5 months ago 25
jQuery Question

jQuery multiple attribute selectors

I have this string

$html = "`<li class="A" test_id=".0.1.$111">
...
<li class="A" test_id=".0.2.$222">`"


and I want to get with jQuery the contents of the class A with test_id=".0.1.$111"

I am trying this but it doesn't work

$(html).find("class[value='A'],test_id[value='.0.1.$111']");


Does anyone know what is wrong ?

Thanks

Answer

The syntax is incorrect for the attribute selectors, you don't need value=. Also there is a specific syntax for selecting by class: .. Try this:

$(html).find(".A[test_id='.0.1.$111']");

Note that adding non-standard attributes to your HTML is invalid, you should prefix them with data-:

$html = '<li class="A" data-test-id=".0.1.$111"><li class="A" data-test-id=".0.2.$222">';
var $li = $(html).find(".A[data-test-id='.0.1.$111']");

You can also use filter():

$(html).find('.A').filter(function() {
    return $(this).data('test-id') == '.0.1.$111';
});
Comments