Setting title field based on a radio button on a node add form in Drupal 7 with JQuery


<?php
function mymodule_form_alter(&$form$form_state$form_id)  {
  
//dsm($form);
  
switch ($form_id)  {
       case 
'ticket_node_form':
            
$form['#attached']['js'][] = drupal_get_path('module''mymodule') . '/node-add.js';
           break;
  }
}
?>
node-add.js:
(function ($) {
  Drupal.behaviors.cgps_helpdesk_misc = {
    attach: function (context, settings) {
      $("#edit-field-radio-1,#edit-field-radio2").change(function ($) {
        if (jQuery("#edit-field-radio-1").attr("checked")) {
          jQuery('#edit-title').val('Some text');
        } else if (jQuery("#edit-field-radio2").attr("checked")) {
          jQuery('#edit-title').val('Some other text');
        }
      });
    }
  };
}(jQuery));
© 2011 Adam Gerson.