jQuery does not works in Infowindow

2,801 views
Skip to first unread message

thebit

unread,
Apr 17, 2011, 4:09:28 PM4/17/11
to Google Maps JavaScript API v3
Hello,
I have another problem: this time the problem is with jQuery.
Please, look this page: http://thebit.altervista.org/maps/testMapAndJQuery.html
If you try to click the marker, in the opened infowindow any buttons
correctly works.
I tried in differents ways to realise a jQuery window but it does not
works.
Please, can you say me why?

Rossko

unread,
Apr 17, 2011, 7:47:20 PM4/17/11
to Google Maps JavaScript API v3
> I tried in differents ways to realise a jQuery window but it does not
> works.
> Please, can you say me why?

The contents of the infowindow (<input>, <a> tags etc.) don't exist in
the DOM until _after_ the infowindow has been opened.
So you can't attach jquery actions to them ahead of time, such as $
('input.alertMe').click(...)

thebit

unread,
Apr 18, 2011, 5:11:40 AM4/18/11
to Google Maps JavaScript API v3
> The contents of the infowindow (<input>, <a> tags etc.) don't exist in
> the DOM until _after_ the infowindow has been opened.
> So you can't attach jquery actions to them ahead of time, such as $
> ('input.alertMe').click(...)

So, How I should to do?
1) Please, can you post a little example or say me in which way I can
open a jQuery Window?
I understand I cannot utilise the syntax "$
('input.alertMe').click(...)".
2) In your opinion, it'better tu utilise "domready" instead of the
native Infowindow?

thanks.

Keith Brown

unread,
Apr 18, 2011, 7:18:23 AM4/18/11
to Google Maps JavaScript API v3
Your jQuery is not working because the elements do not exist in the
DOM when you are running the jQuery code. Move your jQuery code out
of the $(document).ready(function(){}) and into the click listener for
your infowindow.

thebit

unread,
Apr 20, 2011, 7:45:33 AM4/20/11
to Google Maps JavaScript API v3
Thank you very much: now I have understand.
I will try to do that in this way.

Keith Brown

unread,
Apr 21, 2011, 7:33:53 AM4/21/11
to Google Maps JavaScript API v3
Also, inside your click listener, add this second listener and put
your code inside here:

google.maps.event.addListener(infowindow, 'domready', function(){
//jQuery code here
});

without this, sometimes the browser will execute the jQuery before the
elements are added to the dom.

thebit

unread,
Apr 21, 2011, 12:00:57 PM4/21/11
to Google Maps JavaScript API v3
Well!
Thank you for your interesting suggestion!

thebit

unread,
Apr 22, 2011, 2:44:25 PM4/22/11
to Google Maps JavaScript API v3
Well!
It perfectly works!
Thank you so much!

thebit

unread,
Apr 26, 2011, 2:23:16 PM4/26/11
to Google Maps JavaScript API v3
Hello,
I have a last question:
I have the following addListener code
************
google.maps.event.addListener(this.infoWindow, 'domready', function()
{
// jQuery code
$('input.alertMe').click(function()
{
$("#window").dialog("open");
return false;
});
});
***********
How I can to submit some data to the window which will be opened?
E.g.: I wish to send a value in "#window" so I can work with it.

Keith Brown

unread,
Apr 27, 2011, 6:00:25 AM4/27/11
to Google Maps JavaScript API v3
That's kinda vague. I don't really know what you are trying to do.
Post a link to your test map with your failed attempt.

thebit

unread,
Apr 28, 2011, 7:47:39 AM4/28/11
to Google Maps JavaScript API v3
Hello,
please look here: http://thebit.altervista.org/maps/testMapAndJQuery.html
My intention is to pass some datas/variables to the #window because
then, in thid dialog window I must to call a functione and to pass the
variable.
Look the source code, that's more clair.

Eg.: I have a function
/////////////////////
function test(param)
{
alert(param);
}
\\\\\\\\\\\\\\\\\\\\\\\
and then I have
///////////////////////
// Dom
$(document).ready(function()
{
// show the windows
$('#window').dialog(
{
// black bg
modal: true,
autoOpen: false,
buttons:
{
'Cancel': function()
{
$(this).dialog("close");
},

'Confirm': function()
{
test(varToPassExample); // it's the var passed from "domready"
addListener but I don't know how to pass it.
$(this).dialog("close");
}
}
});
}
\\\\\\\\\\\\\\\\\\\\\\\\\\

Keith Brown

unread,
Apr 29, 2011, 6:36:20 AM4/29/11
to Google Maps JavaScript API v3
this not really a google maps question and my solution is definitely
not the best. Maybe somebody can tell you a better way, but here's
what I'd do:

assign a global variable at top of your script
var myVar = null;

when you create your marker (home), add your variable to the marker
array as a key:value pair
var: 1

inside your marker click function, assign the global myVar value with
your home.var value
myVar = home.var;

inside your jQuery dialog confirm function, put
test(myVar);

thebit

unread,
Apr 29, 2011, 1:31:33 PM4/29/11
to Google Maps JavaScript API v3
Hello,
I have yet considered this way to assign a global variable but I don't
wish to create many global variables and for this I'm looking for a
better way.
Reply all
Reply to author
Forward
0 new messages