0

I am trying to display a hidden class in jQuery but it is no applying to the class I am targeting. It displays the whole class:

<div class="feeds">
  <input class="post_id" />
  <textarea class="commentdata"></textarea>
  <button class="mmore">More</button>
  <p class="time">time</p>
  <div class = "comment_data">
    <div class = "comment">
      <p> Comments </p>
    </div>
  </div>
</div>

<div class="feeds">
  <input class="post_id" />
  <textarea class="commentdata"></textarea>
  <button class="mmore">More</button>
  <p class="time">time</p>
  <div class = "comment_data">
    <div class = "comment">
      <p> Comments </p>
    </div>
  </div>

<div class="feeds">
  <input class="post_id" />
  <textarea class="commentdata"></textarea>
  <button class="mmore">More</button>
  <p class="time">time</p>
  <div class = "comment_data">
    <div class = "comment">
      <p> Comments </p>
    </div>
  </div>
</div>

The class comment_data is display: none by default and should only be displayed whenever the More button is clicked. Working but it is displaying all the comments for 3 div.

This is my jQuery code:

$( ".mmore" ).click(function() {
   $('.comment_data').slideToggle("slow");
});

3 Answers 3

6
$('.mmore').click(function() {
   $(this).parents('.feeds').find('.comment_data').slideToggle('slow')
});
Sign up to request clarification or add additional context in comments.

2 Comments

This is more convenient because it less depends on html structure.
This did the trick. You've just sealed my day productively. Thanks a whole lot.
1

You can use .closest() to find .feeds div and then use .find() for *.comment_data* div

$('.mmore').click(function() {
   $(this).closest('.feeds').find('.comment_data').slideToggle('slow')
});

Comments

0

You're selecting all objects matching the selector .comment_data.

You'll need to limit it to the same container that the clicked button is in. There's a number of ways you could do this. One easy one would be:

$( ".mmore" ).click(function() {
   $(this).parent().find('.comment_data').slideToggle("slow");
});

Comments

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.