3

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.

3
  • It looks like you are executing the code when the document is already ready, because otherwise you would see the error "ReferenceError: Invalid left-hand side in assignment". Commented Mar 21, 2013 at 16:59
  • I could be wrong; but either way I'm pretty sure it's a method... Commented Mar 21, 2013 at 16:59
  • Just out of curiosity, and it may just be preference in the end. But why don't you leave the function a basic function (not a binding jq function) and then bind that to your selectors on doc ready. In so doing, you allow yourself the flexibility to reuse your function later or for other events that may crop up in your ui. In short, you decouple the event from the function. Right now it seems tightly coupled and single purposed. $(function() { $('yourselectors').change(function() { yourfunction(this);}) }); Commented Jun 4, 2013 at 18:12

1 Answer 1

14
$.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.

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

1 Comment

I knew one of you jQuery machine's would have me straightened in a matter of seconds.

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.