I'm struggling to get my form to have Ajax run javascript. I can do the replacement example ok that is located at http://api.drupal.org/api/drupal/includes--ajax.inc/group/ajax

And sometimes if I put the output open the output form that in script tags, I'll get some outcomes, but I'll also get a lot of errors.

<script>
 JS in here
</script>

I'm thinking what I want to do is return commands. All I'm trying to do right now is get a google map to recenter. I'm not using the maps module because I want Maps API V3.

My code I'm trying to run right now is:

function mobile_location_form($form, $form_state){
  $form['container']['controls']['submit'] = array(
  		'#type' => 'submit',
  		'#value' => 'Submit',
  		'#ajax' => array(
		      'callback' => 'ajax_example_simplest_callback',
		      'wrapper' => 'replace_textfield_div',
     ),);
	 
	 
$form['container']['controls']['replace_textfield'] = array( // ajax writes our new map locations here.
    '#prefix' => '<div id="replace_textfield_div"> ',
    '#suffix' => '</div>',
  );
  
  return $form;
  
}

function ajax_example_simplest_callback($form, $form_state) {
 
  $commands = array();
  $commands[] = 'oMap.setCenter(google.maps.LatLng(0,0))';
  return array('#type' => 'ajax', '#commands' => $commands);
}

All I want to do is reset the center. Am I missing a step or doing this completely wrong? What would you suggest I do here?

Comments

nevets’s picture

I do not see a div with the id 'replace_textfield_div' and there is this beta module, Google Maps API V3

jeditdog’s picture

Sorry, I had to weed out a lot of the code to only show what I was trying to accomplish. I've updated the example to be more precise with all the relevant parts.

nevets’s picture

Ok, I see two issues.

a) I do not see 'ajax' as a command, Ajax Commands has some convince functions for building a command.

b) A bigger issue is the code is incomplete. I have not tried this scenario so can only point out how it falls short and the approach I would try. You are simply trying to write javascript into the element, so even if that works, there is nothing to trigger the code. Instead what I think you want is to implement a Drupal behavior and "feed" it new data in the callback.