So I have a <ul>
that contains <li>
elements, and I'd like to grab the IDs of these out and pass them on in a querystring to another page.
Like so:
<ul id="myList">
<li id="first">First</li>
<li id="second">Second</li>
<li id="third">Third</li>
</ul>
into
first,second,third
Is there a neat way to do this? I've jQuery, so my brute-force probably-not-very-good-approach is to iterate using each()
and build it that way. A bit scruffy, I think.
A short and neat way using .map
:
var ids = $("#myList li").map(function() {
return this.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