Catfish
Catfish

Reputation: 19284

jQuery <function> is not a function

I'm trying to create a simple text block that updates when 1 of 3 form fields is changed.

Here's my jQuery and it keep getting this error: TypeError: $(...).updateTitlePrefix is not a function

Can anyone tell me what i'm doing wrong here?

$ = jQuery.noConflict();

$(document).ready(function() {

    $('#dataEntryForm\:studyId').updateTitlePrefix();
    $('#dataEntryForm\:formNumberQualifier').updateTitlePrefix();
    $('#dataEntryForm\:formVersionNumber').updateTitlePrefix();
});

// updates the titlePrefix when either the study#, form# or form version are changed    
$.fn.updateTitlePrefix() = function() {
    $(this).change(function() {
        $('#dataEntryForm\:titlePrefix').text($('#dataEntryForm\:formNumberQualifier').text() + $('#dataEntryForm\:formVersionNumber').text() + $('#studyId').text())
    });
}

Not sure if this is relevant, but i'm on jquery 1.3.2 because this is a JSF project and that's what is included in the Richfaces library.

Upvotes: 3

Views: 1401

Answers (1)

Brad M
Brad M

Reputation: 7898

$.fn.updateTitlePrefix = function() {

Remove the parentheses to win.

$.fn.updateTitlePrefix() is a function invocation; since you are just declaring the function, there is no need to invoke/call the function.

This is a commonly seen issue with functions expecting functions as parameters. For example.

function requriesFn(myFunction) {}
requiresFn(doStuff()); // incorrect (passes the return value of the function doStuff)
requiresFn(doStuff); // correct (passes the function itself)

$.ajax({
    error: $.noop() // incorrect
    error: $.noop // correct
});

// and my biggest pet peeve, 
$(document).ready(function() {
    myFunction();  
});
// simplify it
$(document).ready(myFunction);
// simplify it some more
$(myFunction);

Technically, there may be a need to invoke a function to return another function, but that is generally not the case.

Upvotes: 14

Related Questions