Reputation: 46308
I have some jQuery to loop through li
elements of a div
and grab the text of those elements.
I have tried this code:
var liText = $('#4441437 li').each(function() {
$( this ).text()
});
console.log(liText);
When I do this I see this:
Object[li, li, li, li, li, li, li, li, li, li]
I realize that this is a jQuery object that is returned.
My question is how do I store just the text in a variable for each li
element within a div?
Upvotes: 0
Views: 130
Reputation: 62
Try using this:
var liText = [];
$('#4441437 li').each(function() {
liText.push($( this ).text()
});
console.log(liText);
Upvotes: 0
Reputation: 841
You can store text values in Array, or store all content in one variable as one string (not a good idea). I will post both versions:
var contentArr = [];
var fullContent = '';
var liText = $('#4441437 li').each(function() {
contentArr.push($(this).text());
fullContent += $(this).text() + ' ';
});
console.log(contentArr);
console.log(fullContent);
Here is example in JSFiddle
Upvotes: 2