简体   繁体   中英

Show/hide div using trigger with specific class to toggle div with same class

I have my triggers and divs to show/hide in a while loop, instead of using div id which has to unique, i've decided to use div class to show/hide a group of content among others.

what i'm trying to achieve:

i'm not at all good with javascript, and i've been trying to achieve this for days. Say i have a trigger with div class="view1-'.$id1.'" where $id1=2 and div class="licom-'.$cc_id.'" where $cc_id=2 to show/hide, is it possible to ensure that my trigger would only show/hide the div class with the same id as 2 ?.

JavaScript

<html>
<head>
<script language="JavaScript">
$(document).ready(function(){
var showText='View all replies';
var hideText='Hide';
// initialise the visibility check
var is_visible = false;
// append show/hide links 
$('.view1').prev().append();
$(".licom").hide(); 
$(".view1").click(function(){//i need to pass the div class with the variable
// switch visibility
is_visible = !is_visible;
// change the link depending on whether the element is shown or hidden
$(this).html( (!is_visible) ? showText : hideText);
 //i also need to pass the right div class with the right variable, and keep the others hidden
$('.licom').toggle(function() {
$(this).closest('view1').find('.licom').hide();
return false;
 },
function() {
$(this).closest("view1").next(".licom").show();
return false;
 });  
    });          
}); 
</script>
</head>
<body>
</body>
</html>

info.php

  <?php
  ...........
   $stmt = $conn->prepare(
  "SELECT *
  FROM comment
  WHERE post_id = :pid
  ");

$stmt->bindParam(":pid", $type_id, PDO::PARAM_INT);


$stmt->execute();

while($obj = $stmt->fetch()){
     $username = $obj['user_name'];
      $comment = $obj['comment'];
      $id1 = $obj['id'];
      $userimage = $obj['user_image'];

  echo '<div class="txt">';
  echo '<div class="comment-container">';
  echo '<div class="comment-item">';
    echo '<div class="comment-avatar">';
      echo '<img src="user/user_images/'.$userimage.'" alt="avatar">';
    echo '</div>';
     echo '<div class="comment-post">';
      echo '<span style="font-weight:bold;">'.$username.'&nbsp&nbspsaid....  
     </span>';
      echo '<p style="margin-left:-11px;">'.$comment.'</p>';
      echo '<input type="hidden" name="comment_id" value="'.$id.'">';

       //trigger to hide/show replies
             echo '<span class="view1-'.$id1.'" style="float:right;margin-top:-15px;">View all replies</span>';
             //

          echo '</div>';

         echo '</div>';

          echo '</div>';  

         echo '</div>'; 
        echo '</div>';

 //Relpy to comment, show only the first row
 $rep = $conn->prepare("SELECT  * FROM reply WHERE comment_id = :comid LIMIT 1");
$rep->bindParam(":pid", $id1, PDO::PARAM_INT);


$rep->execute();

while($obj = $rep->fetch()){
    //...........same output as first without the view all replies trigger......

//Relpy to comment, show from row 2-
$rep = $conn->prepare("SELECT  * FROM reply WHERE comment_id = :comid LIMIT 1,18446744073709551615");
$rep->bindParam(":pid", $id1, PDO::PARAM_INT);


$rep->execute();

while($obj = $rep->fetch()){

$cc_id = $obj['comment_id'];
//div to show/hide
echo '<div class="licom-'.$cc_id.'">';
 //...........same output as first without the view all replies trigger......

}
  }
     }
 ?>

how do i re-write my JavaScript so that all div of class "licom" are hidden by default, and only the div with the same id as the trigger say 2,3,... as the case maybe would show/hide onClick.

As requested in the comments on question:

"Something like: http://jsfiddle.net/qjadyznh/ ?"

$('a').click(function(){
    var id = $(this).attr('id');
    if($('.'+id).css('display') == 'none')
    {
        $('.'+id).css('display','block');
    }
    else
    {
        $('.'+id).css('display','none');
    }
})

Simple example on how to achieve the goal needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM