Bhavan Kumar Natarajan
Bhavan Kumar Natarajan

Reputation: 367

this jquery not working

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

Answers (3)

Pranav C Balan
Pranav C Balan

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

Dhara Parmar
Dhara Parmar

Reputation: 8101

Try

$(".explore_degree_widget_area li a:has(:contains('Associates'))").addClass('icon_associate');

Upvotes: 0

VisioN
VisioN

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

Related Questions