0

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>

6
  • 4
    Not related to you question: IDs should be unique. Use classes instead. Commented Nov 4, 2017 at 22:20
  • 1
    Repeating an id is never a good idea (should be a unique identifier) Commented Nov 4, 2017 at 22:20
  • 1
    @GerardoFurtado - why is that not related? Commented Nov 4, 2017 at 22:20
  • 1
    Well this is using jQuery which you haven't tagged in your question so be sure to check you have included the jQuery library and you shouldn't duplicate ID's. ID's are supposed to be unique. I would recommend you swap from using id's to using a class name. While testing things in javascript / jQuery I 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. Commented Nov 4, 2017 at 22:20
  • 2
    No way you are getting 1 2 3 4 from that code Commented Nov 4, 2017 at 22:23

3 Answers 3

4

You are seeing the content of the your HTML, and not the console.log.

This is what you need to fix:

  1. Include jQuery
  2. Use class instead of id - there can be only one id in a document, so you'll get only one result
  3. Look at the console

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 class="bla">3</div>
    <div class="bla">4</div>
</div>

Sign up to request clarification or add additional context in comments.

Comments

2

First id has to be unique replace id with class and use your snippet

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 class="bla">3</div>
    <div class="bla">4</div>
</div>

Comments

0

First of all, you can't have two HTML elements with same id attribute. It is unique indeficator, it should be only one on page.

And second, if you are using your js script from external (not inluded between <scritp></script>) you should use (document.ready or in jQuery $(docuemnt).ready(function(){}) [in my case it is short way to do this]), because you don't want to read HTML values before your is not loaded.

(function () {
  var myArray = [];

  $( '.bla' ).each( function() {
    myArray.push( {
      test: $( this ).text(),
    });
  });
  
  console.log( myArray );
}());
<div>
    <div>1</div>
    <div>2</div>
    <div class="bla">3</div>
    <div class="bla">4</div>
</div>

1 Comment

you're doing nothing other than replacing id with class

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.