0

I have a page with following format.

<% (1..3).each do %>
<div class="each-message-content notification-message-content">
 <div class="">
  <div class="">
  </div>
 </div>
 <div class="">
 </div>
 <div class="">
 </div>
</div>
<div class="">
</div>
<div class="read-unread">
</div>
<% end %>

Clicking on read-unread I add a class read or unread. But when I check for if the class has read or unread class it always execute if block. Never enter in else block. How can I do this?

 $(document).on('click','.read-unread', function(){
        var $read_unread = $(this).closest('.notification-message-content');
        if($read_unread.hasClass('unread')){
            $read_unread.removeClass('unread');
            $read_unread.addClass('read');                
        } else if (($read_unread.hasClass('read'))){
            $read_unread.removeClass('read');
            $read_unread.addClass('unread');

        }

    });
1
  • See .toggleClass() (or toggleClassName(), not sure). Commented Mar 19, 2014 at 12:43

3 Answers 3

1

Going with a definite selector might be even better

$(document).on('click','.read-unread', function(){
    $(this).prev().prev().toggleClass('read unread');   ;
});

Do note that one of the 'read' / 'unread' class needs to be already in place.

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

3 Comments

+1 even seems more portable: $(this).siblings('.notification-message-content').toggleClass('read unread');
I think the problem is I am using a loop here. So same class appear 3 times. Thats why none of the answers are working for me. I think we have to find closest class name notification-message-content
@user3128796 If the structure is the same as you mentioned. My answer should work, as it does not depend on the css class.
0

.closest() looks up the DOM tree. .notification-message-content is not up the tree, it is on the same level as .read-unread. Since we know it's above it, you can write

var $read_unread = $(this).prevAll('.notification-message-content');

which will look at all previous siblings in the tree.

1 Comment

I think the problem is I am using a loop here. So same class appear 3 times. Thats why none of the answers are working for me. I think we have to find closest class name notification-message-content
0

Try this:

$(document).on('click', '.read-unread', function () {
    var $read_unread = $(this).prevAll('.notification-message-content').first();
    $read_unread.toggleClass('unread read');
});

DEMO

3 Comments

@Satpal: Added toggleClass() solution also. Thanks Bro. :)
I think the problem is I am using a loop here. So same class appear 3 times. Thats why none of the answers are working for me. I think we have to find closest class name notification-message-content
@user3128796: Please find updated answer with demo link.

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.