Haim Haim - 21 days ago 7
jQuery Question

jQuery if class name contains

Here's my HTML

<div class="item">Item #SKU987</div>


I'm trying to create a simple jQuery, but it keeps on returning
undefined


if (jQuery('div.item:contains("Item #SKU987")').length) {
alert("Hello! This works!");
}


I'm trying this out in the console and I keep on getting
undefined


What am I missing?

Answer

See @HenryDev's second solution. If you run it in the console you will get undefined as result because this the if statement doesn't have value itself. Just ignore this result in the console. When using indexOf you must be aware of the case of the letters. Are you sure the text you are looking for is in the exact case and not lower or upper case? Use view-source: to be sure you are searching for the right thing.