I have the following function ('submitTitle') which is called when a button is pressed. I have two radio buttons which is dynamically generated by 'submitTitle'. When the user selects 'no' radio button, it triggers an annonymous function which also calls within it another function called 'getIndexOfObject'. My problem is, i keep getting the following error: 'Uncaught ReferenceError: getIndexofObject is not defined', even though I have defined it. I do not understand how to fix this.
var titleArray = [];
UploadMyScript.submitTitle = function(id, callback){
console.log('button '+id + ' pressed');
//disable 'append' button.
document.getElementById(id).disabled = true;
var title_;
var titleToAttach = document.getElementById('txtArea_'+id).value;
if(titleToAttach == ''){
console.log('Please enter a title for this pdf');
// un-disable 'append' button
document.getElementById(id).disabled = false;
}else{
/*HANDLE WEBSERVICE CALL HERE FOR EACH TITLE*/
// Replace spaced in title with %20 (pubmed format for spaces)
searchterm_ = encodeURI(titleToAttach);
console.log('searchterm_: '+ searchterm_);
//AJAX CALL
var formdata = new FormData();
formdata.append('titleSearchParam', searchterm_);
// Create XMLHttprequest
var xmlhttp = new XMLHttpRequest;
console.log('pass0000');
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
console.log('greetings form inside onreadystate');
/*RESPONSE TO AJAX CALL HERE*/
var json_obj = JSON.parse(event.target.responseText);
console.log('responseText: '+ event.target.responseText);
var json_obj = JSON.parse(xmlhttp.responseText);
console.log('responseText: '+ xmlhttp.responseText);
if(json_obj == null || json_obj.title == null){
title_ = titleToAttach;
callback(id, title_);
console.log('null, title: ' + title_);
}else{
.......
var radio_no = document.createElement('input');
radio_no.setAttribute('id', 'radio_no_'+id);
radio_no.type = 'radio';
radio_no.name = 'article';
radio_no.value = '0';
radio_no.addEventListener('click', function(){
// Remove this object from the titleArray
var indexOfObject = getIndexofObject(id, titleArray);
titleArray.splice(indexOfObject, 1);
console.log('titleArray below after splice: ');
console.log(titleArray);
});
}
}
}
xmlhttp.open("POST", "upload_myfiles.php");
xmlhttp.send(formdata);
}
}
// Find the Index of a 'title_fileName' object in titleArray
function getIndexOfObject(id, array){
var id_toMatch = UploadMyScript._('paraHeading_'+id).dataset.uniq_id;
for(i=0; i< array.length ; i++){
if(id_toMatch == array[i]['uniq_id']){
return i;
}
}
}