I'm trying to push multiple values into an array based on the elements ID.
I expect the outcome to be 3 and 4 but instead I get 1 2 3 4. Why is that the case in the following example?
var myArray = [];
$( '#bla' ).each( function() {
myArray.push( {
test: $( this ).text(),
});
});
console.log( myArray );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div>
<div>1</div>
<div>2</div>
<div id="bla">3</div>
<div id="bla">4</div>
</div>
jQuerywhich you haven't tagged in your question so be sure to check you have included thejQuerylibrary and you shouldn't duplicate ID's. ID's are supposed to be unique. I would recommend you swap from usingid's to using aclassname. While testing things injavascript/jQueryI would also recommend you open the browser console so you can see any errors that might be reported/given. This will also tell you the cause of error and which line this error has occurred on.1 2 3 4from that code