The following function opens a popup window when I click a link:
function popupPlace(dict) {
$popup = $('div#dish-popup');
$popup.render(dict,window.dishPopupTemplate);
if(typeof(dict.dish) === 'undefined') {
$popup.addClass('place-only');
} else {
$popup.removeClass('place-only');
}
var $place = $('div#dish-popup div.place');
var place_id = dict.place._id;
if(liked[place_id]) {
$place.addClass('liked');
} else {
$place.removeClass('liked');
}
if(dict.place.likes) {
$place.addClass('has-likes');
} else {
$place.addClass('zero-likes');
}
var tokens = window.currentSearchTermTokens;
var tokenRegex = tokens && new RegExp($.map(tokens, RegExp.escape).join('|'), 'gi');
$.each(dict.place.products, function(n, product) {
$product = $('#menu-item-'+product.id);
if(liked[place_id+'/'+product.id]) {
$product.addClass('liked');
}
if(tokens && matchesDish(product, tokens)) {
$product.addClass('matched');
$product.highlight(tokenRegex);
} else {
$product.removeClass('matched');
$product.removeHighlight();
}
if(product.likes) {
$product.addClass('has-likes');
} else {
$product.addClass('zero-likes');
}
});
$('#overlay').show();
$('#dish-popup-container').show();
// Scroll to matched dish
$("a#scrolll").attr("href", "#" + $("li.matched").attr("id"));
$("a#scrolll").trigger("click");
// Hide dish results on mobile devices to prevent having a blank space at the bottom of the site
if (Modernizr.mq('only screen and (max-width: 640px)')) {
$('ol.results').hide();
}
$(".close-dish-popup").click(function() {
$("#overlay").hide();
$("#dish-popup-container").hide();
$('ol.results').show();
changeState({}, ['dish', 'place', 'serp']);
});
showPopupMap(dict.place, "dish-popup-map");
}
At the end you can see the following:
$("a#scrolll").trigger("click");
Everything works but that link is not being triggered as soon as the popup shows up:
$('#dish-popup-container').show();
Any suggestions to fix this?
you are calling the
$("a#scrolll").trigger("click");before assigning$('#dish-popup-container').show();before assigning the click event to the close button.just put the
$(".close-dish-popup").click();out side of the function or top of the function(before calling it)