Ajax Autocomplete Textfield for Nodes

function module_name_form() {
  $form = array();
 
  $form['nid'] = array(
    '#title' => t('Nodes'),
    '#type' => 'textfield',
    '#autocomplete_path' => 'example/autocomplete',
   );
 
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Save',
  );
 
  return $form;
}
function module_name_menu() {  
  $items['example/autocomplete'] = array(
    'page callback' => '_module_name_autocomplete',
    'access arguments' => array('access example autocomplete'),
    'type' => MENU_CALLBACK
  );
  return $items;
}
function _module_name_autocomplete() {
  $matches = array();
 
  // Some fantasy DB table which holds cities
  $query = db_select('node', 'n');
 
  // Select rows that match the string
  $return = $query
    ->fields('n', array('nid', 'title'))
    ->condition('n.title', '%' . db_like($string) . '%', 'LIKE')
    ->range(0, 10)
    ->execute();
 
  // add matches to $matches  
  foreach ($return as $row) {
    $matches[$row->nid] = check_plain($row->title);
  }
 
  // return for JS
  drupal_json_output($matches);

}