how to implement Ajax callback on form_alter for entity field? EDITED: I am able to display image for a field dropdown which have entity reference values to another content type. That is when a node content is edited or created. 

Note: I'm not creating form elements I'm using them.

For a content type I have a field_select dropdown list (entity reference to another content type). I am trying to alter my form, to display the images of the selected entity from dropdown.

I'm new to the ajax callback functions, and need a little help here. I have a searched a lot and succeeded a bit.

output of my dsm($form): 

$form =&gt; array (
['field_select'] =&gt; array (
  ['und'] =&gt; array (
    ['#options'] =&gt; array ( 
      [_none] =&gt; -None-
        [1] =&gt; content1 //1 is key
        [2] =&gt; content2 //2 is key
        [3] =&gt; content3 //3 is key
      )
    )
  )
)


Present ISSUE: When the below drop down is selected to a value of above, Image is being displayed in the dummy field below, but only once. For example if I select 1 (content1) the it renders image from node 1 according to the ajax callback function [success]. But now if I select 2 its not loading image from node2, instead it shows only node1 image which was rendered first.



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

 if ($form_id == "article_node_form"){
   $form['field_select']['und']['#ajax'] = array(
     'callback' =&gt; 'field_select_ajax_callback',
     'wrapper' =&gt; 'another-field-select-image',
   );
   $form['field_select']['und']['#prefix'] = '&lt;div id="another-field-select-image"&gt;';
   $form['field_select']['und']['#suffix'] = '&lt;/div&gt;';

}

function field_select_ajax_callback($form, $form_state) {
  $id = $form_state['values']['field_select']['und'][0]['target_id']; //node id
  $node_id = node_load($id);

  if(is_object($node_id)) {
    $image_uri = $node_id-&gt;field_image['und'][0]['uri'];
    $image_url = file_create_url($image_uri);
    $image = '&lt;img src="' .$image_url. '" /&gt;';
  }  
  return $image;
}


I want to display respective selected dropdown image near to field-select
Posting my own answer, I will be happy if it helps someone. 

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

  if ($form_id == "article_node_form"){
    $form['field_select']['und']['#ajax'] = array(
      'callback' =&gt; 'field_select_ajax_callback', 
      'wrapper' =&gt; 'field-image', 
    );

//Note: this $form_state is set only after ajax function call (field_select_ajax_callback)
    if(isset($form_state['values']['field_select']['und'][0]['target_id'])) {
      $node_id = $form_state['values']['field_select']['und'][0]['target_id'];
      $image_path = get_image($node_id);
      $form['field_image']['und'] = array(
        '#markup' =&gt; '&lt;div class="sponsor-image"&gt; ' .$image_path. ' &lt;/div&gt;',
      );
    }
    $form['field_image']['und']['#prefix'] = '&lt;div id="field-image"&gt;';
    $form['field_image']['und']['#suffix'] = '&lt;/div&gt;';
    return $form;
  }
}

function field_select_ajax_callback($form, $form_state) {
  return $form['field_image']; // very important step, Where I did it differently from previous. 
}

function get_image($id) {
  $node_id = node_load($id);

  if(is_object($node_id)) {
    $image_uri = $node_id-&gt;field_image['und'][0]['uri'];
    $image_url = file_create_url($image_uri);
    $image = '&lt;img src="' .$image_url. '" /&gt;';
  }
  return $image;
}


Yay fixed it.