php - Show comments with jquery

283

I need create a function like a facebook comment. for example if i will click to comment button, it will show me all comments just from same post. i tried to create with jquery, but if i click to button it shows me all comments in all posts. I need just comments where i have clicked not others.

there is show_com.js:

$(".com_but").click(function() {
$('.all_com').show();
});

there is style css:

.all_com{
display:none;
}

and there is index.php:

<button class='com_but' id="<php echo $post_id; ?>">add comment</button>
<script src="show_com.js"></script>
<div class='all_com' id="<php echo $post_id; ?>">
<input type='text'/>
</div>

$post_id is id from post(like a facebook post or twitter).

797

Answer

Solution:

$('.all_com').show(); will show all divs with class 'all_com', wherever they are on the page. But you seem to intend that you want only to display the 'all_coms' that are children of the same parent as the button clicked. Right?

In that case you should have something like:

<script src="show_com.js"></script>
<div class="container">
  <button class='com_but' id="<php echo $post_id; ?>">add comment</button>
  <div class='all_com' id="<php echo $post_id; ?>">
    <input type='text'/>
  </div>
</div>

and your function would be something like:

$(".com_but").click(function() {
  $('.all_com',$(this).parent()).show();
});

this will only show elements of class all_com that are contained within the parent of the clicked button. Hope this helps.

720

Answer

Solution:

If you just want to show the comments that are within the same hierarchy as the click, the general way of doing that is go from the location of the click (denoted by$(this)) up to a common parent in that section of the DOM (using.closest(selector)), then find all items of interest in that common parent (using.find()).

You don't show enough of your HTML for us to know what that common parent selector would be, but the general idea is like this:

$(".com_but").click(function() {
    $(this).closest(common parent selector here).find('.all_com').show();
});

People are also looking for solutions to the problem: php - Narrowing occurred during type inference in CakePHP

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.