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
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>.
JavaScript Code :$("button"). click(function(){ console. log($("li"). length); });
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(',');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With