• List 1
  • List 2Maybe I need to clear ...">

    Get id of first UL LI using jQuery

    <ul id="someList"> <li id="listItem1">List 1</li> <li id="listItem2">List 2</li> </ul> 

    Maybe I need to clear my selectors, but how would you grab the first ul # someList id?

    Also, if I add LI to ul # someList, will I need to capture the first LI identifier in a different way?

    +4
    source share
    4 answers

    $('ul#someList li:first') is what you are looking for.

    +24
    source

    Best use first selector

     var id = $("ul#someList li:first").attr("id"); 

    or

     var id = $("ul#someList li:first").get(0).id; 
    +2
    source

    This may be another solution:

     $('#someList li:nth-child(1)').attr("id"); 
    +2
    source

    Try it ****. slice (start, [end]) **** `

     <ul> <li id='l1'>list item 1</li> <li id='l2'>list item 2</li> <li id='l3'>list item 3</li> <li id='l4'>list item 4</li> <li id='l5'>list item 5</li> </ul> $('li').slice(2).attr('id'); 
    +1
    source

    Source: https://habr.com/ru/post/1316724/


    All Articles