I am doing a comment system for my webpage. The website shows a group of elements from the database, that everyone can comment:
Element 1 ($element_id=1)-> Read comments...
Element 2 ($element_id=2)-> Read comments...
Element 3 ($element_id=3)-> Read comments...
When someone want to read comments of one element, can click in the "Read comments...", and a new div opens:
<div class="comments_more" id="comments_more"> Read comments...
<div class="comments_div" id="comments_div" >
<?php include/comments.php ?>
<?php echo $element_id; ?>
</div> </div>
The jquery code to open the div works perfect for every element:
$(document).ready(function(){
$( '.comments_more' ).click(function() {
$(this).find( '#comments_div' ).show('slide');
});
})
Now, I found a nice jquery to close the div when user clicks outside of it:
$(document).mouseup(function (e){
var container = $("#comments_div");
if (!container.is(e.target) // if the target of the click isn't the container...
&& container.has(e.target).length === 0) // ... nor a descendant of the container
{ container.hide(); }
});
The problem is that it only works for the first "Read comments..." (first element).