Hi all! I’m hoping someone here can help me out with this issue that’s been vexing me.
I’ve got a Google map with markers (using gmaps.js) that, when clicked, hit a PHP script, query a database and returns info in a modal window - ideally, anyway. It works standalone (ie, with the country data hardcoded for each marker) but when I try to hook up the AJAX and do it all dynamically, the modal comes up empty, giving me a “TypeError: openOverlay(…) is undefined” error in the console.
When I hit the PHP query script manually, it works flawlessly so I can’t figure out what’s going wrong with the JS part. Can anyone tell me why this isn’t jiving?
// modal
function openOverlay(olEl) {
$oLay = $(olEl);
if ($('#modal-shade').length == 0)
$('body').prepend('<div id="modal-shade"></div>');
$('#modal-shade').fadeTo(300, 0.6, function() {
var props = {
oLayWidth : $oLay.width(),
scrTop : $(window).scrollTop(),
viewPortWidth : $(window).width()
};
var leftPos = (props.viewPortWidth - props.oLayWidth) / 2;
$oLay
.css({
display : 'block',
opacity : 0,
top : '-=300',
left : leftPos+'px'
})
.animate({
top : props.scrTop + 40,
opacity : 1
}, 500);
});
}
function closeOverlay() {
$('.modal').animate({
top : '-=300',
opacity : 0
}, 400, function() {
$('#modal-shade').fadeOut(300);
$(this).css('display','none');
});
}
$('#modal-shade, .modal a').on('click', function(e) {
closeOverlay();
if ($(this).attr('href') == '#') e.preventDefault();
});
// interactive map
var map;
$(document).ready(function(){
map = new GMaps({
el: '#map',
lat: 13.2133,
lng: 18.3381,
zoom: 3,
scrollwheel: false,
zoomControl: false,
streetViewControl: false,
mapTypeControl: false,
overviewMapControl: false
});
map.addMarker({
lat: 34.5333,
lng: 69.1333,
title: 'Afghanistan',
click: function(e){
var a= $(this).attr("title");
$.ajax({
url: 'inc/parser.php?country='+a,
type: 'POST',
data: a,
});
openOverlay('.modal').html(data); // This is where I'm getting the error
preventDefault(e);
}
});