Normally, if I want to inject a Javascript snippet into a form, I use a hook like in an extension like this: function alternateduplicatecheck_civicrm_buildForm($formName, &$form) { if($formName == 'CRM_Contact_Form_Inline_CustomData'){ CRM_Core_Resources::singleton()->addScriptFile( 'org.cpehn.alternateduplicatecheck', 'alternate_duplicate_button.js'); } } This works on most forms; however, this doesn't appear to work for forms used for inline editing loaded via AJAX. The hook is called, but the Javascript doesn't run. Maybe I need to modify my Javascript, insert an onLoad in there? It looks like:

cj(function ($) { 'use strict'; console.log('hi'); cj('.crm-button-type-upload').hide(); });

Here's the AJAX request (it's a custom field set on the summary tab): http://<sitename>/civicrm/ajax/inline?cid=5652&class_name=CRM_Contact_Form_Inline_CustomData&groupID=10&customRecId=339&cgcount=1&snippet=6&reset=1

2 Answers 2


cj(function ($) {}) has an implicit document.ready. To unpack that dense few characters of code:

  • cj is the deprecated alias of CiviCRM's copy of jQuery. In 4.5 and above use the new one: CRM.$.
  • Passing a function straight into jQuery is an alias of calling jQuery(document).ready() (not one that we made up, it's a jQuery thing).
  • The argument your function receives ($) is yet another alias of jQuery, which you should use inside your code block to make it more readable (so your code inside should be $('.crm-button-type-upload... not cj(...).
  • So the whole thing, written out in full would be CRM.$(document).ready(function($) {$('.crm-button-type-upload').hide();}); (not that I'm saying you can't use the shorter form CRM.$(function($)... I just wanted to make it clear what's going on)

To answer your question about how to include your js. As of 4.5, CRM_Core_Resources will add javascript files to snippets, and by default it does. I just tested it out and was able to get a script file loaded when I clicked to open an inline edit form on the contact summary page.

The only time this wouldn't work is if you specify the "header" region as the location for your script. This is because ajax snippets do not have a header.

You can find out more about loading javascript files and ajax at: http://wiki.civicrm.org/confluence/display/CRMDOC/Resource+Reference

  • Ah. As you might have guessed from my use of cj, this is for Civi 4.4. I guess I should have specified. I'll save this project for a post-4.6 upgrade! Mar 26, 2015 at 22:59

I ran into a similar situation where my code would work in Firebug or Chrome JavaScript console but if I used cj(function(){});, the page level scripts were still loading and my code came in too early to have the page level scripts in scope.

In my case I used the following:

    // your code

It's worth giving that a try.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.