I want make an array that contains all of the text from the span elements that have the shopping-item class. My attempts to do so have resulted in either the array containing [Object object] or [object HTMLSpanElement]. I just want the text which that span element contains, nothing else.
I've tried this implementation and others from Stack. I've tried using the map method, currently I tried using the toArray method in an effort to view what was being stored inside of the array. Some of the code I have tried is below.
<div class="container">
<h1>Shopping List</h1>
<form id="js-shopping-list-form">
<label for="shopping-list-entry">Add an item</label>
<input type="text" name="shopping-list-entry" id="shopping-list-entry" placeholder="e.g., broccoli">
<button type="submit">Add item</button>
</form>
<ul class="shopping-list">
<li>
<span class="shopping-item">apples</span>
<div class="shopping-item-controls">
<button class="shopping-item-toggle">
<span class="button-label">check</span>
</button>
<button class="shopping-item-delete">
<span class="button-label">delete</span>
</button>
</div>
</li>
<li>
<span class="shopping-item">oranges</span>
<div class="shopping-item-controls">
<button class="shopping-item-toggle">
<span class="button-label">check</span>
</button>
<button class="shopping-item-delete">
<span class="button-label">delete</span>
</button>
</div>
</li>
<li>
<span class="shopping-item shopping-item__checked">milk</span>
<div class="shopping-item-controls">
<button class="shopping-item-toggle">
<span class="button-label">check</span>
</button>
<button class="shopping-item-delete">
<span class="button-label">delete</span>
</button>
</div>
</li>
//Here are some of my attempts for context.
//1ST attempt
const list = $('span.shopping-item').toArray();
for (prop in list) {
alert(`${prop} = ${list[prop]}`);
}
//another attempt
let data = $('li').children('span.shopping-item').map(function() {
return {
item: $(this).text()
};
}).get();
//another attempt
const list = ('ul.shopping-list').children('span.shopping-item').map(function(item) {
return item;
});
I expect an output of:
['apples', 'oranges', 'milk', 'bread']
When I display the list with alert(). However, this is what I get [Object object], [object HTMLSpanElement] or sometimes just a whole list full of [Object object]