:contains
is a too general selector. If it matches, it'll literally select every single element that contains a string - in a chain.
Say you're looking for :contains('foo')
<body> <!-- Yep contains "foo" therefore gets selected -->
<div> <!-- Yep contains "foo" therefore gets selected -->
I'm a DIV
<p> <!-- Yep contains "foo" therefore gets selected -->
foobar <!-- humm... foobar? :D -->
</p>
</div>
</body>
by simply doing
var $els = $(':contains("foo")')
console.log( $els )
you'll see that it's actually an Array of elements. .attr()
immediately filters the first one. Clearly not the one you tried to find (body
).
If you know exactly the tag you're trying to query try to be more specific with your selector like $(".product-info > a > h3:contains('foo')")
.
Otherwise, loop-filter all your elements, make sure that .text()
returns in it's entirety the desired string.
Search products by name: solution
You already have the data-alpha="PRODUCT NAME / 1"
- so use it!
var $products = $("[data-alpha]");
$("#search").on("input", function() {
$products.hide().filter((i, el) =>
new RegExp(this.value.trim(), 'ig').test(el.getAttribute("data-alpha"))
).show();
});
// on scroll don't forget to update $products
/*QuickReset*/ *{margin:0;box-sizing:border-box;} html,body{height:100%;font:14px/1.4 sans-serif;}
#product-loop {
display: flex;
flex-flow: row wrap;
}
[data-alpha] {
flex: 1 1 25%;
border: 1px solid #ddd;
padding: 5px;
}
[data-alpha]:before{
content: attr(data-alpha);
}
<input type="text" id="search" placeholder="Search product...">
<div id="product-loop" class="clearfix">
<div data-alpha="TRI-PACK PERTEX JACKET HIBISCUS" data-price="13800" data-i="1"></div>
<div data-alpha="ZIP KNIT PEACH / RED" data-price="10800" data-i="12"></div>
<div data-alpha="S-PLAKET OVERSHIRT RED" data-price="12800" data-i="13"></div>
<div data-alpha="S-PLAKET OVERSHIRT NAVY" data-price="12800" data-i="14"></div>
<div data-alpha="DANSE SHIRT BLUE" data-price="11800" data-i="15"></div>
<div data-alpha="DANSE SHIRT BLACK" data-price="11800" data-i="16"></div>
<div data-alpha="SERVICE SHORT SLEEVE SHIRT GREEN / WHITE" data-price="9800" data-i="17"></div>
<div data-alpha="SERVICE SHORT SLEEVE SHIRT BLACK / WHITE" data-price="9800" data-i="18"></div>
<div data-alpha="PLAIN PANT BLACK" data-price="12800" data-i="19"></div>
<div data-alpha="PEAKING JEAN MULTI" data-price="15800" data-i="20"></div>
<div data-alpha="P-CARP PANT BLACK" data-price="12800" data-i="21"></div>
<div data-alpha="P-CARP PANT NAVY" data-price="12800" data-i="22"></div>
<div data-alpha="DANSE PLAIN PANT BLACK" data-price="12800" data-i="23"></div>
<div data-alpha="PALACE JEAN BLACK STONEWASH" data-price="12800" data-i="24"></div>
<div data-alpha="PALACE JEAN STONEWASH" data-price="12800" data-i="25"></div>
<div data-alpha="PJS PALACE JEAN WHITE DENIM" data-price="12800" data-i="26"></div>
<div data-alpha="P-LITE RUN IT JACKET ULTRAMARINE" data-price="14800" data-i="27"></div>
<div data-alpha="P-LITE RUN IT SHORT ULTRAMARINE" data-price="9800" data-i="28"></div>
<div data-alpha="P-LITE RUN IT JACKET BLACK" data-price="14800" data-i="29"></div>
<div data-alpha="P-LITE RUN IT SHORT BLACK" data-price="9800" data-i="30"></div>
</div>
<script src="//code.jquery.com/jquery-3.3.1.js"></script>