简体   繁体   中英

Getting value form closest div

I Try to getting the text form same div on button click event. Here is my HTML Markup:

<div class="flights">
    <div class="flight-box">
        <div class="row">
            <div class="col-sm-3">
                <div class="flight-info">
                    <div class="left-i">
                        <img src="img/sp_trans.gif" class="airlogowidth airsprite airlogo A4">
                       <div class="flight-no">SG-264</div>
                    </div>
                    <div class="right-i">
                        <div class="name">Air India</div>
                    </div>
                </div>
            </div>
            <div class="col-sm-5">
                <div class="flight-duration">
                    <div class="row">
                        <div class="col-sm-4">
                            <div class="events">
                                <span class="text">Depart</span>
                                <span class="time">12:30 PM</span>
                                <span class="route">IXA <i class="fa fa-arrow-right"></i> CCU</span>
                            </div>
                        </div>
                        <div class="col-sm-4">
                            <div class="events">
                                <span class="text">Arrive</span>
                                <span class="time">03:10 PM</span>
                                <span class="route">IXA <i class="fa fa-arrow-right"></i> CCU</span>
                            </div>
                        </div>
                        <div class="col-sm-4">
                            <div class="events">
                                <span class="text">Duration</span>
                                <span class="time">1h 40m </span>
                                <span class="route">No Stop</span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-sm-4">
                <div class="fare-price">
                    <div class="row">
                        <div class="col-sm-6">
                            <span class="f-price">3999</span>
                        </div>
                        <div class="col-sm-6">
                            <div class="book-action">
                                <div class="btn-group-vertical" role="group">
                                    <button type="button" class="btn btn-danger btn-book" name="booknow">Book Now</button>
                                    <button type="button" class="btn text-primary btn-more-1" name="details">View More...</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

    </div>
    <div class="flight-footer">
        <div class="row">
            <div class="col-sm-3">
                <div class="refund-status">
                    <span>Refundable</span>
                </div>
            </div>
            <div class="col-sm-3">
                <div class="fare-role">
                    <a href="#">Fare rules</a>
                </div>
            </div>
            <div class="col-sm-3">
                <div class="baggage-info">
                    <a href="#">Baggage Information</a>
                </div>
            </div>
            <div class="col-sm-3">
                <div class="itinerary-info">
                    <a href="#">Flight itinerary</a>
                </div>
            </div>
        </div>
    </div>
    <div class="flight-itinerarySummary" style="display: none;">
    <div class="row">
        <div class="col-sm-12">
        <h2>Agartala → Bangalore <small>22 Nov 2015</small></h2>
        <ul class="itinerarySummary">
            <li class="vendor">
                <div class="airLogo fLeft">
                    <img src="img/airlines/AI.png" height="23" width="27">
                </div>
                <div class="airlineName">
                 <span class="name">Air India</span>
                 <small class="flightNumber">AI-744</small>
                 <small class="travelClass">Economy</small>
                 <small class="truncate" title=""></small>
               </div>
            </li>
            <li class="start">
                <time>
                 <span class="placeTime">
                    <span rel="tTooltip" original-title="Singerbhil, Agartala">IXA</span>
                    <strong>  11:20 </strong>
                 </span>
                 <span class="travelDate">22 Nov 2015</span> 
                </time> 
                <small class="terminal"> 

                  Singerbhil, Agartala
                </small>
            </li>
            <li class="details">
                <i class="clk itineraryClock"></i><abbr class="duration weak">50m</abbr> 
            </li>
            <li class="end">
               <time>
                <span class="placeTime">
                 <strong> 12:10 </strong>
                 <span rel="tTooltip" original-title="Netaji Subhas Chandra Bose Airport, Kolkata">CCU</span>
                </span>
                <span class="travelDate"> 22 Nov 2015 </span>
               </time>
               <small class="terminal">
                  Netaji Subhas Chandra Bose Airport, Kolkata, Terminal 2 
               </small>
            </li>
        </ul>
        <div class="connector weak">
            <small class="layOver">Layover : 5h 20m</small>
        </div>
        <ul class="itinerarySummary">
            <li class="vendor">
                <div class="airLogo fLeft">
                    <img src="img/airlines/AI.png" height="23" width="27">
                </div>
                <div class="airlineName">
                 <span class="name">Air India</span>
                 <small class="flightNumber">AI-744</small>
                 <small class="travelClass">Economy</small>
                 <small class="truncate" title=""></small>
               </div>
            </li>
            <li class="start">
                <time>
                 <span class="placeTime">
                    <span rel="tTooltip" original-title="Singerbhil, Agartala">IXA</span>
                    <strong>  11:20 </strong>
                 </span>
                 <span class="travelDate">22 Nov 2015</span> 
                </time> 
                <small class="terminal"> 

                  Singerbhil, Agartala
                </small>
            </li>
            <li class="details">
                <i class="clk itineraryClock"></i><abbr class="duration weak">50m</abbr> 
            </li>
            <li class="end">
               <time>
                <span class="placeTime">
                 <strong> 12:10 </strong>
                 <span rel="tTooltip" original-title="Netaji Subhas Chandra Bose Airport, Kolkata">CCU</span>
                </span>
                <span class="travelDate"> 22 Nov 2015 </span>
               </time>
               <small class="terminal">
                  Netaji Subhas Chandra Bose Airport, Kolkata, Terminal 2 
               </small>
            </li>
        </ul>
        </div>
    </div>
    </div>
</div>

I want "flight-no" div html ie SG-264 on 'btn-book' on Click. I try as follows, but return 'undefine' -

$('.btn-book').on('click', function(){
 var flightNo = $(this).closest('div.flight-info').parent('div.left-i').html();
 alert(flightNo);
});

Note that in the page have many rows with 'flights' class. Anyone help me?

The issue with your current code is that the .flight-info element is not a direct parent of .btn-book , it's a sibling of one of the parents. This is why the .flight-info selector on closest() returns nothing.

The easiest way to achieve what you require is to use closest() to get the nearest common parent of both elements, in this case .flight-box , then use find() and text() to get the flight number. Try this:

$('.btn-book').on('click', function(){
   var flightNo = $(this).closest('div.flight-box').find('.flight-no').text();
   alert(flightNo); // = SG-264 given your example
});

Example fiddle

you need to use .find() with .closest() and div.left-i is children of div.flight-info not parent()

use it like this

$('.btn-book').on('click', function(){
 var flightNo = $(this).closest('.flight-box').find('div.left-i .flight-no').html();
 alert(flightNo);
});

DEMO

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