Reputation: 134
<script>
function show(id){
$("button").click(function(){
$("p") .toggle();
});
});
</script>
<?php
$bid=0;
$bid=0;
include 'model.php';
$db=new database;
$r=$db->msghead($admno);
while($row= mysql_fetch_array($r))
{
$id=$row[0];
$title=$row[1];
$msg=$row[2];
$date=$row[3];
$sender=$row[4];
$tit_status=$row["title_status"];
$bid=$bid+1;
$pid=$pid+1;
?>
<button style="width:80%;height:35px;" onclick="MessageDetailsById(<?php echo $id;?>)" >
<?php
if($tit_status=="1"){
?>
<i class="fa fa-plus-circle" aria-hidden="true" style="width:20px;float:left;"></i> <i class="fa fa-envelope-open-o" aria-hidden="true"></i> <span id="active" class="sidebar-title" style="color:red;"><?php echo $title; ?></span> <?php echo $date;?>
<?php }else{?>
<i class="fa fa-plus-circle" aria-hidden="true" style="width:20px;float:left;"></i> <i class="fa fa-envelope-o" aria-hidden="true"></i> <span id="active<?php echo $tit_status;?>" class="sidebar-title"><?php echo $title; ?></span> <?php echo $date;?>
<?php }?>
</a></button>
<p style="display:none;" id="<?php echo $id; ?>"> <?php echo $msg; ?>
<a class="btn btn-primary" data-toggle="collapse" href="#collapseExample" aria-expanded="false" aria-controls="collapseExample">
Reply
</a>
</p>
<?php } ?>
so many buttons generated according to database.. but when i click single button all paragraph get open but i want it open only the paragraph related to its button.. i dont know which id will be used and how ???
Upvotes: 4
Views: 1436
Reputation: 72299
What you want(toggle current and hide all at the same time) for that you have to change your structure and do like below:-
Example with your structure (you have to put your dynamic data carefully):-
$('.item p').hide();
$('.item button').click(function(e){
e.preventDefault();
var $this = $(this).parent().find('p');
$(".item p").not($this).hide();
$this.toggle();
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="item"> <!-- this is needed otherwise not possible -->
<button>Button</button> <p>span1</p> <br>
</div>
<div class="item">
<button>Button</button> <p>span2</p><br>
</div>
<div class="item">
<button>Button</button> <p>span3</p><br>
</div>
<div class="item">
<button>Button</button> <p>span4</p><br>
</div>
Reference taken:- http://jsfiddle.net/BGSyS/3/
Upvotes: 2
Reputation: 11
Try something like:
<button ... data-id="<?php echo $id; ?>" ...>...</button>
<p ... data-id="<?php echo $id; ?>" ...>...</p>
And then On click do something like:
$('button').click(function(){
$('p').find("[data-id='" + $(this).data('id') + "']").toggle();
});
I have not tested but I do use this case with dynamic elements in my programs.
Upvotes: 1
Reputation: 7256
you are assigning id to p
<p style="display:none;" id="<?php echo $id; ?>">
so just change your jquery code to this :
function MessageDetailsById(id){
$("button").click(function(){
$("#"+id) .toggle();
});
}
Upvotes: 1