0

I am running a $.each on my JSON data which is getting parsed, the next thing im trying to do in that $.each is check the data from the JSON output against the .text() of each div which have id's of "status".

This is where the problem is coming in, it is only reporting the first div's .text() for each div entry with the id of "status".

$.each(JSON.parse(data), function(idx, value){ 
    console.log(value.id, value.status)
    $('#status').each(function() {
        alert($(this).text());
    });

at first I tried to just run the $.each on the JSON parse and inside that do a if

if(value.status== 'online') {

but that also just returned the first div for each div entry...

Please can someone assist me with this and point out where im going wrong

Sayajin
  • 33
  • 2
  • 9
  • `$('#status').each(` that means you have multiple elements with same id? – void Mar 05 '18 at 16:41
  • "each div which have id's of "status"." and "$('#status').each" makes no sense - you can't have more than one element with the same ID. Otherwise it's no longer an ID (unique identifier) is it? And JavaScript will simply ignore any elements with duplicate IDs, as it considers them invalid. You'd be better off substituting this with a class which can be given to multiple elements. – ADyson Mar 05 '18 at 16:43

1 Answers1

1

Thanks ADyson your answer was spot on, once I made the id's unique it just worked as it should.

"each div which have id's of "status"." and "$('#status').each" makes no sense - you can't have more than one element with the same ID. Otherwise it's no longer an ID (unique identifier) is it? And JavaScript will simply ignore any elements with duplicate IDs, as it considers them invalid. You'd be better off substituting this with a class which can be given to multiple elements.

Sayajin
  • 33
  • 2
  • 9