Haim Haim - 1 year ago 156
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

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

What am I missing?

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download