Dan
Dan

Reputation: 2174

targeting parent div does not work

I could not able to target parent div when a button is clicked within a div, it suppose to target div with id itemsMainDiv

Here is the code used when a button is clicked

<div class="Carousel" data-items="1,3,5,6" data-slide="1" id="itemsMainDiv">
<div class="row">
   <div class="col-md-6">
      <h4 class="txtlabel">More Books By Same Author</h4>
   </div>
   <div class="col-md-6 CarouselNav" style="margin-top:-20px;text-align: right;"> 
      <button id="leftLst" class="btn btn-primary leftLst"><</button>   
      <button id="rightLst" class="btn btn-primary rightLst">></button> 
   </div>
</div>
<br/>
<div class="Carousel-inner">
------
------
------

Screenshot of Code comparison

Jquery Code

 function click(ell, ee) {
   alert("ell="+ell+", ee=" + ee); 
   var Parent = "#" + $(ee).parent().attr("id");
   alert("---Parent="+Parent);
   var slide = $(Parent).attr("data-slide"); 
   ResCarousel(ell, Parent, slide);
}

What i have tried : var Parent = "#" + $(ee).parent('div:first').attr("id"); //Gives null,

Demo example from : http://bootsnipp.com/snippets/zDQkr On this demo example the scroll buttons are placed below. While from my code,i am trying to place it above carousel.

Upvotes: 1

Views: 91

Answers (1)

Curiousdev
Curiousdev

Reputation: 5788

Use .closest to find nearest parent div with class Carousel which you want to target to find.. after putting dis you don't have worry about number of DIVs

function click(ell, ee) {
   alert("ell="+ell+", ee=" + ee); 
   var Parent = "#" + $(ee).closest(".Carousel").attr("id");
   alert("---Parent="+Parent);
   var slide = $(Parent).attr("data-slide"); 
   ResCarousel(ell, Parent, slide);
}

Upvotes: 1

Related Questions