0

I have looked through other post, but couldn't figure this out. I am getting an array of values from JQuery. I have to call a function which makes an ajax request for each item in the array. I need the total score for each item to be return so I can push it to an array. I am having trouble setting up my callback function and keep getting undefined.

$('#btnSave').click(function() {
  //creates array of id's
  var checkedValues = $('.checkedbox:checked').map(function() {
    return this.id;
  }).get();
  var scoreArr = []
  for (i = 0; i < checkedValues.length; i++) {
    //calls function with array 
    printEvaluation(checkedValues[i], 'cb')

    console.log(totalEvalScore) //gives me UNDEFINED
    scoreArr.push(totalEvalScore)
  }
})



function printEvaluation(id, callback) {
  //some code here

  $.ajax({
    url: queryURL,
    method: 'GET'
  }).done(function(res) {

    //response and some more code
    //score is calculated
    var totalEvalScore = calculatedscore

    function cb() {
      return totalEvalScore
    }

    cb()
  })
}
Joe
  • 80,724
  • 18
  • 127
  • 145
Edwin Rodriguez
  • 153
  • 1
  • 4
  • 11

0 Answers0