Skip to content
Advertisement

JQUERY for loop not outputting integers within the array

I tried this code below but the logged output of the loop is coming up as 3649 which are not id numbers in the array being passed. Even when adding more ids or subtracting ids from the array, it does not change the output of the ajax code.

I have confirmed the array is being received properly by the function below by using a console.log(id). This verifies the output as below:

The array as being passed:

 console.log(cids);
(3) […]
​
0: "45"
​
1: "47"
​
2: "46"
​
length: 3

What might I be doing wrong?

function func2(cids) {
    console.log(cids);
      for(let id of cids)
        var parent = document.getElementById('mydiv_' + id)
        console.log(id);
      }
}

If I console.log(id); I get 3649 which isn’t even in the array

Advertisement

Answer

You’re missing a curly brace. Try:

function func2(cids) {
    console.log(cids);
    for(let id of cids){ //this one is missing
        var parent = document.getElementById('mydiv_' + id)
        console.log(id);
    }
}
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement