I want to store all the values of text inside an array in a loop, is it possbile? can someone give me a hint on how to do this?
$('#wa li').each(function (i) {
var text = $(this).text();
});
Just use .map and .get – there's no need for intermediate variables or loops
const liTexts =
$ ('#wa li')
.map ((idx,elem) => $(elem).text ())
.get ()
console.log (liTexts)
// [ 'one', 'two', 'three' ]
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="wa">
<li>one</li>
<li>two</li>
<li>three</li>
</ul>
.map() is the way to go here.)map's callback incorrect, before I checked jquery's documentation and realized their api is just backwards. Also, nice to meet you @nnnnnnthis to refer to the element, so then if you want the index too you can just define that one argument in your callback.this, 20% closures, 20% typos, 20% didn't read easily accessible documentation, 20% code this for me. (OK, I shouldn't have said 100%, there are good, non-duplicate questions too.)