jayant rawat jayant rawat - 6 months ago 11
jQuery Question

How to find parent id by click on child id and then child id by getting parent id via jquery

I am creating commenting module on product page in which i am showing option of spam comment and there i am showing onclick show and hide popup. now it is working fine in single comment but when comment is two it opens both on one time because the class is same. Now that's why i want to get the parent li id so we can find only the internal class of that id not the another one

Here is my jsfiddle link

this is my script

$(".slidermenudown .fa-angle-down").on("click",function(){

if($(".anthr_usr_cmnt").hasClass("active")){

$(".anthr_usr_cmnt").removeClass("active");

}else{
if($(".owner_cmnt").hasClass("active")){
$(".owner_cmnt").removeClass("active");
}
$(".anthr_usr_cmnt").addClass("active");
}

});

Answer

Try this , I have modified your fiddle itself and your css also.

https://jsfiddle.net/Lse3s3uv/3/

.active{
  display:block;
}

    $(".slidermenudown .fa-angle-down").on("click", function() {

  var $this = $(this).parents('.comment'); // "this" is the click's target. 
  console.log($this);
  var $anthr = $this.find(".anthr_usr_cmnt");
  console.log($anthr);
  var $owner = $this.find(".owner_cmnt");
  console.log($owner);

  if ($anthr.hasClass("active")) {
    $anthr.removeClass("active");
  } else {
    if ($owner.hasClass("active")) {
      $owner.removeClass("active");
    }
    $anthr.addClass("active");
  }
});