Get an array of list element contents in jQuery

javascript, jquery

Solution

var optionTexts = [];
$("ul li").each(function() { optionTexts.push($(this).text()) });

...should do the trick. To get the final output you're looking for, `join()` plus some concatenation will do nicely:

var quotedCSV = '"' + optionTexts.join('", "') + '"';

Problem

I have a structure like this: ``` <ul> <li>text1</li> <li>text2</li> <li>text3</li> </ul> ``` How do I use javascript or jQuery to get the text as an array? ``` ['text1', 'text2', 'text3'] ``` My plan after this is to assemble it into a string, probably using `.join(', ')`, and get it in a format like this: ``` '"text1", "text2", "text3"' ```

Original source

Related problems