I'm simply just trying to push the text of an li element when I click the add button. But when I click my add button, it is pushing all the items in my list to the array. I guess it's because I'm just using a class. What would be the best way around this? Thanks in advance!
HTML
<div id="products">
<ul>
<li><p>Lorem 1</p><button class="target">Add</button></li>
<li><p>Lorem 2</p><button class="target">Add</button></li>
</ul>
</div>
jQuery
$(document).ready(function(){
var array = Array();
$(".target").click(function() {
$('p').each(function (i, e) {
array.push($(e).text());
});
console.log(array);
});
});
$('p')selects all p elements. You want to select only the one that came before the clicked element..prev