1

I have some problem with changing <span> content </span>.

I get response from AJAX but i can't change <span> element.

Here is my html:

<?php $i = 1; ?>
@foreach($Hotels as $hotel)
  <img src="{{ asset($hotel->img) }}" width="800" height="533" class="img-responsive" alt="{{ $hotel->HotelName }}">
  <div id="score"><span>{{ $hotel->score }}</span>{{ $hotel->rating }}</div>
  From/For Night
  <span class="price">
    <sup>$</sup>
    <span id="H{{ $i }}" data-hotel-id="{{ $hotel->HotelID }}">...</span>
  </span>
  <div class="hotel_title">
    <h3>{!! preg_replace("/ /", " </strong>", '<strong>'.$hotel->HotelName, 1 ) !!}</h3>
  <div class="rating">{{ $hotel->HotelClass }}</div>
  </div>
<?php $i++; ?>
@endforeach

And my JS:

$(document).ready(function(){
  for(var i=1; i<=6; i++){
    var data = $('#H'+i).data('hotelId');
    $.get('/online/prices/'+data, function(prices){
        //Try to change my span 
        $('#H'+i).text(prices);
    });
  }
});

But if i change $('#H'+i).text(prices); to $('#H1').text(prices); it will work only for <span id='#H1'>.

UPDATE

  • you never close the div with class "hotel_title". don't think that matters for your problem but still good to do. – thanksd Dec 15 '15 at 14:27
  • 1
    Not familiar with Laravel, but you set i = 1 but never iterate it in your for each loop, so I think it sets id to H1 in each loop. Think there might need to be an $i++ in there somewhere maybe? – Bearcat9425 Dec 15 '15 at 14:31
  • @Bearcat9425 - nothing to do with Laravel, you hit the nail on the head. The value of `$i` is not being changed in the loop. – Kryten Dec 15 '15 at 14:36
  • So you added some code to increment `$i` - did that fix it? I see the question has been closed as a duplicate - others think your Javascript is the problem. Is that the problem? – Kryten Dec 15 '15 at 17:54
  • @Kryten yes the problem was in JS. – Evgeny Zmanovsky Dec 16 '15 at 11:28

0 Answers0