views:

40

answers:

1

Hi

Why is focus not set to noteTitle?

I am using google maps API V3.

The getNoteForm() returns a input field "noteTitle".

The $("#noteTitle").focus() works fine when executed in firebug.

I call this function when clicking on the map:

function setNewNoteInfowindow(latlng) {
 if (geocoder) {
       geocoder.geocode({'latLng': latlng}, function(results, status) {
   var address = "";

     if (status == google.maps.GeocoderStatus.OK) {
    if (results[1]) { 
     address =  results[1].formatted_address;
            }
         }

   newNoteInfowindow = new google.maps.InfoWindow({
    content: getNoteForm(latlng, address),
       size: new google.maps.Size(40,50)
   });

   newNoteInfowindow.open(map, newNoteMarker);

   google.maps.event.addListener(newNoteInfowindow, 'domready', function() {
          $("#noteTitle").focus();
      });

   google.maps.event.addListener(newNoteInfowindow, 'closeclick', function() {
          newNoteMarker.setVisible(false);
      });
  });
 }
}
A: 

It looks like you are opening your infowindow before adding the event listener. So the event has already been fired by the time you add the listener. Try reversing the order of open() and addListener()

   google.maps.event.addListener(newNoteInfowindow, 'domready', function() {
          $("#noteTitle").focus();
      });

   newNoteInfowindow.open(map, newNoteMarker);
Mark
Yes, I tried that as well... No luck.
thomas
Solved it like this. Not pretty, but it works.google.maps.event.addListener(map, 'click', function(event) { placeNewNote(event.latLng); setTimeout(function(){$("#noteTitle").focus()}, 500);});
thomas