Loop inside the submit button

Asked

Viewed 62 times

0

I have the submit button that must submit the grade to a class of students. But when I click the button, it only submits the grade to the last student on the list. The student list and note list are stored in the Students array and grid array. I’m using a loop for this, but I’m not getting the whole class to get their grades. My function this JS and the button calls the function in HTML. How do I make the loop add the note to all students?

function addGradeToUsers(){
   showData();
   var students = [58686,58687, 392854];  // user Ids
   var orgUnitId= 167877;   //course number
   var gradeObjectId =111321; //grade name
   var gradeValue= [34,10,33];


    for(var i=0; i< students.length; i++){ 
    document.getElementById("PUTField").checked = true;
    document.getElementById("actionField").value = "/d2l/api/le/1.0/"+ orgUnitId +"/grades/"+ gradeObjectId+ "/values/"+ students[i] +"";
    document.getElementById("dataField").value = "{\n \"Comments\":{\"Content\":\"null\",\"Type\":\"Text\"},\n \"PrivateComments\":{\"Content\":\"null\",\"Type\":\"Text\"},\n \"GradeObjectType\": 1,\n\"PointsNumerator\":"+gradeValue[i]+" \n}";  
    } 
}

And here’s the boot code:

<div class="form-group">
<label for='contentType'>Importa notas</label>
<input class="btn btn-default" type="button" value="Add Grade to Users" onclick='addGradeToUsers()'>
  </div>
  • Place the HTML where these selected elements are in document.getElementById("PUTField") and others. I suspect you are duplicating these id’s and this may be the cause of the problem.

  • @Sam didn’t understand what you meant. Putfield is a low-key radio button when I click to submit notes. PUT comes from the API.

  • I found it strange that the tie is always selecting the same elements.

  • @Sam "Putfield" means the API will make a PUT. PUT will be every note.

  • The for is asynchronous with anything. From what I understand, the result of this for will only be applied to the last element of the array, i.e. 392854.

  • From what I understand, there’s an API listening to the checked true and will process the changed values of the id’s, is that it? If so, it doesn’t work because the for is asynchronous with other processes. If so, I believe I would have to use an API callback to process each thing at once.

  • @Sam Yes, and that’s right. I have an API listening to checked true and it will process the changed values of id’s. How do I implement callback? any hint /

  • I just got here. Man, what an API this is?

Show 3 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.