user2060802 user2060802 - 1 year ago 105
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


Does anyone know what is wrong ?


Answer Source

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:


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';