Poldo
Poldo

Reputation: 1932

How to get the data from the parent class using jQuery

Please, help me I'm having trouble on getting data from my parent div class. Can someone help me on how can I get the data. Thanks in advance.

Here is the HTML:

<div class="item hasTooltip slick-slide tooltipstered slick-current slick-active slick-center" data-id="579" data-startmonth="December" data-name="Jom X,sk" data-address=" " data-birthdate="December 21, 2016" data-birthplace="Bangladesh" data-mobile="" data-email="" data-year="2016" data-month="12" data-day="20" data-itemcount="38" data-createintake="John B. Doe" data-attorney="Laura V. Conley" data-slick-index="37" aria-hidden="false" tabindex="0" role="option" aria-describedby="slick-slide037" style="width: 136px;">
    <div class="appointment-date">            <span>Dec. Tue</span>
        <label class="day">20</label>
    </div>
    <h1>•</h1>
    <div class="intake-details">
        <a class="remove-text-decoration" href="http://papersllc.com/carecen/client-profile/579" tabindex="0"><i class="user-initials" data-id="579" data-status="15">JX</i></a>
        <span class="name">Jom X,sk</span>
        <span class="sched">11AM Sksk</span>
        <a class="gotoCalendar" tabindex="0">3 more</a>
    </div>
</div>

Here is my JavaScript:

$('.item .intake-details .gotoCalendar').on('click',function(){
    alert($(this).parent('.item').data('name'))
});

.item class is the parent class in my query above so when I click the a tag having .gotoCalendar class is I want to get the data form .item class. Please help me, thanks.

Upvotes: 0

Views: 356

Answers (3)

Poldo
Poldo

Reputation: 1932

Opps I get it now hahaha i forgot to add another .parent()

$('.item .intake-details .gotoCalendar').on('click',function(){
       alert($(this).parents().data('name'))
});

Upvotes: -1

sameer
sameer

Reputation: 320

Please try using the below code

$('.item .intake-details .gotoCalendar').on('click',function(){
    alert($(this).closest('.item').data('name'));
});

Upvotes: 2

Madhusudan Nagar
Madhusudan Nagar

Reputation: 71

No need to add multiple parent() function you can use parents() method to get any level of parent object.

$('.item .intake-details .gotoCalendar').on('click',function() {

   alert($(this).parents(".item").data('name'))

});

Upvotes: 0

Related Questions