Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get all li inside ul using jquery

Tags:

jquery

i have a list with ul and li as:

<ul id="list1"> 
<li id="item-1">List Item 1</li> 
<li id="item-2">List Item 2</li> 
<li id="item-3">List Item 3</li> 
<li id="item-4">List Item 4</li> 
<li id="item-5">List Item 5</li> 
<li id="item-6">List Item 6</li></ul>

I need to get all the ids of the li inside ul as comma separated string using jquery like item-1,item-2,item-3,item-4,item-5,item-6

like image 568
Prasad Avatar asked Nov 23 '09 10:11

Prasad


People also ask

How to get all li elements of ul JavaScript?

In JavaScript, you can use the . getElementsByTagName() method to get all the <li> elements in <ul>. In-addition, you can use the . querySelectorAll() method also to get all the <li>.

How can get Li count in jquery?

JavaScript Code :$("button"). click(function(){ console. log($("li"). length); });


1 Answers

Use $.map for a nice and short way of doing that:

var liIds = $('#list1 li').map(function(i,n) {
    return $(n).attr('id');
}).get().join(',');
like image 122
karim79 Avatar answered Sep 22 '22 18:09

karim79