Adding ajax to text field in node edit form I have created a content type "Type A" and added three fields to content type. I want to display value in third field equal to (value of first field - value of second field).

I want to implement the same using ajax functionality. I have created a custom module and alter the "node form" and add ajax to field 1 but ajax is not working on change event of textfield.

Code Snippet:

function employee_login_form_alter(&amp;$form, &amp;$form_state, $form_id){

    $form['field_total_value']['#prefix'] = '&lt;div id="calculate-total-value"&gt;';
    $form['field_total_value']['#suffix'] = '&lt;/div&gt;';


    $form['field_value_first']['#ajax'] = array(
        'callback' =&gt; 'employee_login_ajax',
        'wrapper' =&gt; 'calculate-total-value',
        'method' =&gt; 'replace',
        'event' =&gt; 'change',    
    );

    $form['field_value_first']['und'][0]['#ajax'] = array(
        'callback' =&gt; 'employee_login_ajax',
        'wrapper' =&gt; 'calculate-total-value',
        'method' =&gt; 'replace',  
        'event' =&gt; 'change',        
    );

}

function employee_login_ajax(&amp;$form, &amp;$form_state) { //print_r($form); exit;
  print_r($form_state['values']);
}
You don't need ajax, you can do this with jQuery or javascript. Something like this might work.

First, create an external js file and set up your change events.

jQuery(document).ready(
    function()
    {
        jQuery("#field2").change(subtract_fields);
        jQuery("#field1").change(subtract_fields);

        function subtract_fields()
        {
            if (jQuery("#field1").value != "" &amp;&amp; jQuery("#field2").value != "")
            {
                return jQuery("#field1").value - jQuery("#field2").value;
            }

            return "";
        }
    }
);


Next, load the external js file using drupal_add_js.

Clear your cache and voila.