Todd Vance
Todd Vance

Reputation: 4711

Is there anything wrong with sending in an OPTIONAL parameter to a javascript function - testing for undefined

Im filling in dropdowns with ajax'd info -- sometimes (when editing) I not only want to fill in the dropdown but choose the value ... so I have added an optional parameter - and check for IF != UNDEFINED ... is there anything WRONG with this approach?

It seems to work.

function getFormFields(TypeOfFields, NameOfElement, OptionalValue) {
    $.ajax({
        type: 'POST',
        url: 'WorkflowWizard.aspx?TemplateWorkflowID=' + $("#hiddenTemplateWorkflowID").val(),
        data: {
            'LinkFormField': true,
            'TypeOfFields': TypeOfFields
        },
        success: function (data) {
            $("#vizLoadingDiv").hide();
            $("#" + NameOfElement).html(data);
            if (OptionalValue != undefined) {
                $("#" + NameOfElement).val(OptionalValue);
            }
        },
        error: function (xhr) {
            alert(xhr.responseText);
        }
    });
}  

Upvotes: 0

Views: 49

Answers (1)

HBP
HBP

Reputation: 16033

Depends. You can call the function with three parameters the last one of which has value undefined - this may or may not be a problem. The "proper" way of handling this is is check arguments.length which will be 2 or three depending on how many parameters you actually used.

Upvotes: 1

Related Questions