Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jun 2013
    Location
    Calgary AB,Canada
    Posts
    55
    Thanks
    28
    Thanked 1 Time in 1 Post

    Getting the first and last index of direct child DIV

    Hi,


    I have the following code :

    Code:
                <div id="MainWrapper">
    
                    <div class="Holder">
                        <div class="LBox"><img src="s05.jpg" /></div>
                        <div class="RBox">Picture 5 description</div>
                    </div>
                    
                    
                    <div class="Holder">
                        <div class="LBox"><img src="s06.jpg" /></div>
                        <div class="RBox">Picture 6 description</div>
                    </div>                
                    
                    
                    <div class="Holder">
                        <div class="LBox"><img src="s07.jpg" /></div>
                        <div class="RBox">Picture 7 description</div>
                    </div>                
                    
                    
                    <div class="Holder">
                        <div class="LBox"><img src="s08.jpg" /></div>
                        <div class="RBox">Picture 8 description</div>
                    </div>                
    
                </div>
    Here we see, that the main DIV identified by "MainWrapper" has 4 direct children having the class "Holder".

    I need to get the index of first and the last direct children. How can I do that in JQuery. I did the following but does not work:

    Code:
    $(document).ready(pageLoad); 
    
    function pageLoad () {
    	First_Indx = $("#MainWrapper div:first").index(); 
    	Last_Indx = $("#MainWrapper div:last").index();
    }

    Please help me...

    thanks a lot,


    Isaac

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,373
    Thanks
    11
    Thanked 592 Times in 572 Posts
    the index of the first one is 0, and :last-child will find your last one. :first-child works if you have junk your're not showing before the first inner div.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.6, IE11:9.1, IE9:3.1, IE10:3.0, FF:17.2, CH:46, SF:11.4, NON-MOUSE:38%


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •