Brett
Brett

Reputation: 20049

ReferenceError: function is not defined - but it is

I have this javacsript function and it has been working fine, but then I added some code to it and unless I comment out that code it gives the error:

ReferenceError: updateListingForm is not defined

The full code is:

function updateListingForm(type) {

    if (jQuery('#voucher_value').length){
        var voucher_value = jQuery('#voucher_value').val();
    } else {
        var voucher_value = null;
    }

    if (jQuery('#category_id')val().length > 0 && isNaN(jQuery('#category_id').val()) == false) {
        var category_id = jQuery('#category_id').val();
    } else {
        var category_id = 0;
    }

    var loadUrl    = relative_path + 'ajax_files/change_listing_type.php'; 
    var dataObject = { type: type, category_id: category_id, voucher: voucher_value }

    getAjaxData(loadUrl, dataObject, 'GET', 'json')

        .done(function(response) {

            console.log(response);

            // Add/remove the listing field stuff
            jQuery('#listing_type').html(response.listing_type);

            // Input addl cat fee
            jQuery('#addl_cat_fee').html(response.addl_cat_fee);

        })

        .fail(function() {
            alert('There seems to have been a problem changing the listing type; please contact us if this continues to occur.');
        });

    // End    

}

If I comment out the below code it works fine:

if (jQuery('#category_id')val().length > 0 && isNaN(jQuery('#category_id').val()) == false) {
    var category_id = jQuery('#category_id').val();
} else {
    var category_id = 0;
}

However to get it to work fine I obviously need to add this line if commenting out the above:

var category_id = 0;

What is wrong with my code that causes it to give this error?

Upvotes: 0

Views: 1519

Answers (2)

apsillers
apsillers

Reputation: 115930

jQuery('#category_id')val() is a syntax error. You want jQuery('#category_id').val().

You see the error updateListingForm is not defined because the syntax error halts the parser and aborts the creation of the function.

The function-not-defined error occurs when you try to use the function. You should also see a prior error in your console from when the parser tried to create the function: either Uncaught SyntaxError: Unexpected identifier in Chrome, SyntaxError: missing ) after condition in Firefox, or Expected ')' in IE. That error should point you to the exact line of the problem.

Upvotes: 6

Sushanth --
Sushanth --

Reputation: 55740

You have an error in the first condition of the if statement.

jQuery('#category_id').val().length
                      ^------Missing this

Upvotes: 3

Related Questions