0

Drupal 7. I have a radio button set. When one of the items is selected I would like it to set the node add title to some string. This is as far as I got, but I get the error: "TypeError: '[object Object]' is not a function (evaluating '$("#edit-field-i-need-help-with-und-printer-ink")')"

(function ($) {
      Drupal.behaviors.cgps_helpdesk_misc = {
        attach: function (context, settings) {
          $("#edit-field-i-need-help-with-und-printer-ink").change(function ($) {
              if ($("#edit-field-i-need-help-with-und-printer-ink").attr("checked")) {
                alert('Handler for .change() called.');
                $('#edit-title').val($('#edit-title').val() + 'bla bla bla');
              }
          });
        }
      };
    }(jQuery));
  • 2
    Remove the $ from the chane handler parameter list. – mpdonadio Mar 6 '13 at 2:33
1

What MPD is saying is change this line:

$("#edit-field-i-need-help-with-und-printer-ink").change(function ($) {

to this:

$("#edit-field-i-need-help-with-und-printer-ink").change(function () {

You have declared two functions as $ and that may be killing the script.

  • I did try that and it still threw an error – AdamG Mar 6 '13 at 12:24
  • What is the error? Because the answer above is correct. If anything it should be .change(function(event){}) because an event object is passed to that function, That is why $ no longer works in the function and you have to use jQuery instead, the $ variable is now a an event object, not a jQuery object. – 2pha Mar 6 '13 at 12:34
  • Actually, I'll just post the whole code that should work as an answer. – 2pha Mar 6 '13 at 12:34
1
(function ($) {
  Drupal.behaviors.cgps_helpdesk_misc = {
    attach: function (context, settings) {
      $("#edit-field-i-need-help-with-und-printer-ink").change(function (eventObject) {
          if ($("#edit-field-i-need-help-with-und-printer-ink").attr("checked")) {
            alert('Handler for .change() called.');
            $('#edit-title').val($('#edit-title').val() + 'bla bla bla');
          }
      });
    }
  };
}(jQuery));

The only part that was wrong in the original is .change(function ($) This part is overriding the $ variable which is a jQuery object and setting it to an event object. The change event send an event object to the listener functions.

The question is already answered, but I thought it would be good to try to explain what is actually going on to give the op a better understanding

0

Got it working. I have a feeling the jQuery substitution of the $ is not the "Drupal 7 way", and I would be curious to know what that way is, but this works:

(function ($) {
      Drupal.behaviors.cgps_helpdesk_misc = {
        attach: function (context, settings) {
          $("#edit-field-i-need-help-with-und-printer-ink").change(function ($) {
              if (jQuery("#edit-field-i-need-help-with-und-printer-ink").attr("checked")) {
                alert('Handler for .change() called.');
                jQuery('#edit-title').val(jQuery('#edit-title').val() + 'bla bla bla');
              }
          });
        }
      };
    }(jQuery));

Full solution at http://agerson.net/setting-title-field-based-radio-button-node-add-form-drupal-7-jquery

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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