0

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;
        }   
    }
}
James Thorpe
  • 31,411
  • 5
  • 72
  • 93
dave
  • 475
  • 6
  • 17
  • 1
    I think what you're looking for are along these lines http://stackoverflow.com/questions/2900839/how-to-structure-javascript-callback-so-that-function-scope-is-maintained-proper – ds011591 Jun 22 '15 at 18:14
  • 2
    **JavaScript is case-sensitive**, you have mistyped _getIndex**o**fObject_, but it is defined as _getIndex**O**fObject_ – jherax Jun 22 '15 at 18:16

1 Answers1

4

The error is correct, it's not defined.

You're calling getIndexofObject, and the function name is getIndexOfObject.

The O is capitalized.

Josh Beam
  • 19,292
  • 3
  • 45
  • 68
  • 3
    FWIW, there's a dedicated close reason for typos :) – James Thorpe Jun 22 '15 at 18:12
  • @JamesThorpe, lol good point – Josh Beam Jun 22 '15 at 18:14
  • 2
    Thanks man. you are correct. The reason why i didn't think it was a typo is because when doing a search using the Bracket text editor, it highlight both the typo error and the correct method name, making me think that they were identical. – dave Jun 22 '15 at 18:18