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 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    922
    Thanks
    302
    Thanked 3 Times in 3 Posts

    Question jQuery: Getting a count of LIs

    Hello

    I wanted to get a count of all the LIs until <li id="end">Test </li>


    So the alert should prompt 7 instead of 12. I am trying the following code but does not seem to work. The code is prompting me 1. Can you pls help me?

    Code:
    <script src="jquery.js"></script>
    
    <script>
    	
    	$(document).ready(function(){
    	
    		var xx = $('ul li#end').length;
    		
    		alert(xx);
    	
    	});
    
    
    </script>
    
    <ul>
    	<li>Test </li>
    	<li>Test </li>
    	<li>Test </li>
    	<li>Test </li>
    	<li>Test </li>
    	<li>Test </li>
    	<li id="end">Test </li>
    	<li>Test </li>
    	<li>Test </li>
    	<li>Test </li>
    	<li>Test </li>
    	<li>Test </li>						
    </ul>
    Thanks
    Last edited by phantom007; 01-24-2012 at 04:57 PM.

  • #2
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,680
    Thanks
    6
    Thanked 1,009 Times in 982 Posts
    Use index().

  • #3
    Regular Coder
    Join Date
    Jun 2006
    Location
    UK
    Posts
    922
    Thanks
    302
    Thanked 3 Times in 3 Posts
    Also, is there anyway to find out the total number of LIs after <li id="end">Test </li> ?

    So it should be 5 in the above case.


    Thanks.
    Last edited by phantom007; 01-25-2012 at 05:12 AM.

  • #4
    The fat guy next door VIPStephan's Avatar
    Join Date
    Jan 2006
    Location
    Halle (Saale), Germany
    Posts
    8,680
    Thanks
    6
    Thanked 1,009 Times in 982 Posts
    Try it with nextAll() and length.


  •  

    Posting Permissions

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