Hi boycetrus and welcome to SitePoint,
The reason as to why your above code isn't working correctly is due to how you have setup the element for the toggle event, instead of targeting specific elements by their class name or ID you really should be using some built into jQuery methods which help with situations like this. See the below for a version of the above code that should work fine.
Code JavaScript:
$('.event-map').each(function() {
// Hide the map by default
$(this).hide();
// Add a button before the map to toggle it
$('<span />', {
'class' : 'toggle-map',
html : 'Show Map'
}).on('click', function() {
// Cache the jQuery object for the map
var $map = $(this).next('.event-map');
// Toggle the map
$map.slideToggle();
// Change the text on the toggle button
$(this).text(($map.is(':visible') ? 'Show' : 'Hide') + ' Map');
}).insertBefore(this);
});
Bookmarks