Reputation: 21
I need to check if inside a
tags, contains specific text
I am using this kind of code, but it return success message every time.
if($('span.selectBox-label:contains("Segmentation")')){
console.log('selected');
}else{
console.log('not selected');
}
this is my html
<a class="selectBox required selectBox-dropdown selecterror selectBox-menuShowing" tabindex="NaN">
<span class="selectBox-label" style="width: 293px;">List Management</span>
<span class="selectBox-arrow"></span>
</a>
Upvotes: 0
Views: 496
Reputation: 11693
Use
if($.trim(('span.selectBox-label').text()) == 'Segmentation' ){
Upvotes: 0
Reputation: 388436
Because $(selector)
will return a jQuery object, which will always be truthy. You can either check whether the length of the returned value is > 0
or use .is()
like
if ($('span.selectBox-label').is(':contains("Segmentation")')) {
console.log('selected');
} else {
console.log('not selected');
}
Also note that, using :contains() will return partial matches
Upvotes: 3