Reputation: 367
I m using jQuery for my drupal site. I can't add class to current element by using jQuery.
I put this addclass inside a if condition with alert. Alert has working fine. But addclass not working.
if (jQuery(".explore_degree_widget_area li a").has(":contains('Associates')")) {
jQuery(this).addClass('icon_associate');
alert('Yes');
}
Can you help me?
Upvotes: 1
Views: 77
Reputation: 115212
If condition would be always true, since has()
returns filtered jQuery object. Also has()
avoid text nodes, it's only check on it's descendant, You need to use filter()
.
jQuery(".explore_degree_widget_area li a").filter(":contains('Associates')").addClass('icon_associate');
or use combined selector
jQuery(".explore_degree_widget_area li a:contains('Associates')").addClass('icon_associate');
Upvotes: 3
Reputation: 8101
Try
$(".explore_degree_widget_area li a:has(:contains('Associates'))").addClass('icon_associate');
Upvotes: 0
Reputation: 145368
this
refers to the context but not to the element. Use the following:
jQuery(".explore_degree_widget_area li a:contains('Associates')")
.addClass('icon_associate').each(function() {
alert('Yes'); // if you really want to have it
});
Upvotes: 1