Updates are as follows:
Dom cleanup --Removal of the use of innerHTML usage in refreshStudents() as it could cause issues with getElementById(). Reference(Why is "element.innerHTML+=" bad code?) Removal of un-necessary variables. Added handleAs so that the page knows how to process the response and set a timeout.
Update to HTML to call refreshStudents() only when work is relevant for team work. In instances where team work is relevant, Call validateForm() only after successful response from refreshStudents(). If team work is not relevant for the type of work then validateForm().
function refreshStudentsAndValidate()
{
<s:if test="work.isTeam">
dojo.byId("studentsDiv").innerHTML="";
var classId = dojo.byId("classId").value;
var url = "<s:url action='LoadStudents' namespace='/files' includeParams='none'/>?classId=" + classId;
dojo.xhrGet({
url: url,
handleAs: "text",
timeout: 10000, //Time in milliseconds (10 secs)
load: function(result)
{
try
{
dojo.html.set(dojo.byId("studentsDiv"), result);
// now call validation...
validateForm();
}
catch (Exception)
{
createDojoAlert("Refresh students (catch block)","<li>Error: " + Exception + "</li>");
}
},
error: function(error)
{
createDojoAlert("Refresh students (dojo error)","<li>Error: " + error + "</li>");
}
});
</s:if >
}
Updates to HTML as follows, use of struts2:
<td align="center">
<input type="button" class="button" name="Checkin" value="Complete" onclick="dojo.byId('completing').value='true'; <s:if test="work.isTeam"> refreshStudentsAndValidate();</s:if><s:else> validateForm(); </s:else>" />
</td>
In summary, the root cause was the refreshStudents() function. Due to the use of dojo.xhrGet making an asynchronous request the page was not receiving the response before the validation code was executed. Because we only need to check the students for cases where the work is team related the struts2 if/else was added for use of the appropriate function when the user presses the button to complete the work.