3

I am adding classes namely hide_some_features,show_some_features in each loop. Now i want to add class to feature_suffix having next only hide_some_features class. I tried with $(this).closest('.feature_suffix').addClass('test-class'); which is not working.

 $('.variations_form input:radio[name=attribute_pa_1]').each(function() {
     if (jQuery.inArray($(this).attr('data-feature-value'), disabledFetaureValue) < 0) {
         $(this).closest('.cc-selector').addClass('hide_some_features');
         $(this).closest('.feature_suffix').addClass('test-class');
     } else {
         $(this).closest('.cc-selector').addClass('show_some_features');
     }
 })

; enter image description here

I want to add class to highlighted place

6
  • 1
    Question is not clear for me, can you please explain more? Commented Sep 24, 2016 at 6:29
  • can you share the html as text not as image Commented Sep 24, 2016 at 6:31
  • Simple is that I want to add a class feature_suffix near with hide_some_features Commented Sep 24, 2016 at 6:31
  • 1
    I think , question is to just add class .feature_suffix to previous div whenever the next div contains .hide_some_features ?? is it ? Commented Sep 24, 2016 at 6:31
  • 1
    try this, $(this).closest('.cc-selector').prev().addClass('hide_some_features'); becuase closest() method fine only parent div and you want to select outside of parent div Commented Sep 24, 2016 at 6:33

2 Answers 2

2

.closest() wont work here because the target element is not an ancestor of the element that fires the event. Use the below:

$(this).closest('.cc-selector').prev('.feature_suffix').addClass('test-class');

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Really you are a star. :)
1

You can find the closest cc-selector, then see whether it has hide_some_features, if so then add the new class to its previous sibling

$(this).closest('.cc-selector').filter('.hide_some_features').prev('.feature_suffix').addClass('test-class');

1 Comment

P Johnty Love you :) Really genious

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.