HEEN
HEEN

Reputation: 4721

Calling javascript function from another function is not working

I want to use the JsonObject value in a function which is defined in another function, so I am using it like below.

function downloadPDF(e) {
setImagesUploadedFile(objValue); // another function
var obj = objValue[0].Filename;
var filePath = SharedFilePath + obj;
$('#ImgSignedDoc').attr('href', filePath);}

And that function setImagesUploadedFile(objValue); is here like this

function setImagesUploadedFile(JsonObject) {
for (i = 0; i < JsonObject.length; i++) {
    var obj = JsonObject[i].Filename;
    var obj2 = "ImgSignedDoc";
    var obj3 = JsonObject[i].FileType;
    var datafileName = JsonObject[0].ImageName;

    var ImgObj = parent.document.getElementById(obj2);
    var Filename = datafileName.substring(datafileName.lastIndexOf('//') + 1);
    ImgObj.innerText = Filename;
    $(ImgObj).attr("data-filename", Filename);
}

}

and the error it shows is

0x800a1391 - JavaScript runtime error: 'objValue' is undefined

So what should I do or change in order to get the parameteres of setImagesUploadedFile(objValue) in my another function ??

Upvotes: 0

Views: 88

Answers (3)

Mohan Lal
Mohan Lal

Reputation: 1

You need to define objValue before passing into function. Eg:

function downloadPDF(e)
{ 
    var objValue = {key1: value, key2: value};
    setImagesUploadedFile(objValue); // another function var obj = 
    objValue[0].Filename; var filePath = SharedFilePath + obj; 
    $('#ImgSignedDoc').attr('href', filePath);
}

Upvotes: 0

Keith
Keith

Reputation: 155692

The problem is before you even start: objValue is never declared and populated.

function downloadPDF(e) {
    // At this point objValue is undefined
    setImagesUploadedFile(objValue); // another function

    var obj = objValue[0].Filename;
    var filePath = SharedFilePath + obj;
    $('#ImgSignedDoc').attr('href', filePath);
}

Try using 'use strict' declarations to better errors here as it will cause errors on the undeclared variable reference, rather than when it is used.

I think you're trying to treat objValue as a reference - that works for edits but not instantiation (JS has no out parameter), so use a return instead:

function downloadPDF(e) {
    'use strict';
    var json = // However you get your JSON data...
    var objValue = setImagesUploadedFile(json); 
    ...

Then, in setImagesUploadedFile:

function setImagesUploadedFile(jsonObject) {
    'use strict';
    var result = [];
    if(jsonObject) // Check we were passed the object
        for (var i = 0; i < jsonObject.length; i++) {
            ...
            var imgObj = ...

            // Add the result to the expected output array
            result.push(imgObj);
        }

    // Return the output array
    return result; 
}

So, steps to fix this:

  • Add 'use strict'; to get better errors.
  • Declare variables before you use them.
  • Use return from a function to get values out.

Finally, as a side note, there's a convention in JS to use camelCase for functions and variables, and only use CapitalCase for classes and functions that expect to be called with new. You don't have to use it, but it can help make your code easier to understand.

Upvotes: 1

Khizar Iqbal
Khizar Iqbal

Reputation: 495

function downloadPDF(e) {
    // objValue is not declared, is this a global variable, if yes then declare it before calling the downloadPDF function.
    setImagesUploadedFile(objValue); // another function
    var obj = objValue[0].Filename;
    var filePath = SharedFilePath + obj;
    $('#ImgSignedDoc').attr('href', filePath);
}

var objValue = [];
downloadPDF(e);

or you can get the values of the objValue from where you want like:

var objValue = $("#id or class");
downloadPDF(e);

Upvotes: 0

Related Questions