-1

On response i got multiple items, i want to append every item in response on h2 class. My code simply loop every items in one class <h2 id="test">

this is my try:

$(function() {

  var $test = $('#test').one();

  $.ajax({

    type: 'GET',
    url: 'test.json',
    contentType: "application/json; charset=utf-8",
    dataType: "json",

    success: function(tests) {
      <!-- <!-- console.log(gares) --> -->

      $.each(tests, function(index, item) {
        $tests.append('<h2>' + item[1] + '</h2>');
      });
    }

  });

});
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha384-tsQFqpEReu7ZLhBV2VZlAu7zcOV+rXbYlF2cqB8txI/8aZajjp4Bqd+V6D5IgvKT" crossorigin="anonymous"></script>

<h2 id="test"></h2>
<h2 id="test"></h2>
Alexandre Elshobokshy
  • 10,720
  • 6
  • 27
  • 57
JavaCrypT
  • 11
  • 6
  • 2
    Please do NOT [use the same id for more than 1 element](https://stackoverflow.com/questions/5611963/can-multiple-different-html-elements-have-the-same-id-if-theyre-different-eleme#5611973) – Alexandre Elshobokshy Jan 22 '19 at 14:44
  • `contentType: "application/json; charset=utf-8",` — You are making a GET request. There is no content to specify the type of. – Quentin Jan 22 '19 at 14:55

1 Answers1

0

It loops over 1 item only because you're using id and not class, try changing your HTML to :

<h2 class="test"></h2>
<h2 class="test"></h2>

And then your JS should look like this :

var $test = $('.test')

I would suggest reading this.

Alexandre Elshobokshy
  • 10,720
  • 6
  • 27
  • 57