0

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?

1
  • There is no "." in your code jQuery('#category_id')val().length . Is it a copying error ? Commented Jul 11, 2013 at 16:57

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

Comments

3

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

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

2 Comments

I think I have to +1 just for that arrow
I so wanted to accept your answer because of the arrow lol, but had to go with apsillers as he gave a good explanation haha

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.