Toby Cook Toby Cook - 1 year ago 71
HTML Question

jQuery - Links Stop Working After Page Resize

I am trying to make my website responsive and have added some jQuery to remove and insert HTML. I am trying to display a popup box when an anchor tag around an image is clicked.

The window appears the first time the page is loaded, but when it has been resized the link no longer works and doesn't display the popup.

// Call hidePopup function upon clicking element with class ".close"
$(function() {
function hidePopup() {
if ( == 'hideP1') {
} else if ( == 'hideP2') {
} else if ( == 'hideP3') {

// Call showPopup function upon clicking element with class ".imgClick"
$(function() {
function showPopup() {
if ( == 'imgEms') {
} else if ( == 'imgPas') {
} else if ( == 'imgTs') {
var LinkedHtml = [
'<a href="#popup1" class="imgClick" id="imgEms" onclick="showPopup()"><img src="assets/img/festival-big.jpg" title="Event Medical Services"></a>',
'<a href="#popup2" class="imgClick" id="imgPas" onclick="showPopup()"><img src="assets/img/ambulance2.png" title="Private Ambulance Services"></a>',
'<a href="#popup3" class="imgClick" id="imgTs" onclick="showPopup()"><img src="assets/img/training_1.jpeg" title="Training Solutions"></a>'
var nonLinkedHtml = [
'<img src="assets/img/festival-big.jpg" title="Event Medical Services" id="emsImg">',
'<img src="assets/img/ambulance2.png" title="Private Ambulance Services" id="pasImg">',
'<img src="assets/img/training_1.jpeg" title="Training Solutions" id="tsImg">'

$(window).resize(function() {
windowSize = $(window).width();
var anchorIds = ['#imgEms','#imgPas','#imgTs'];
var imageIds = ['#emsImg','#pasImg','#tsImg'];

if (windowSize < 740) {
for (var i = 0; i < imageIds.length; i++) {
} else if (windowSize > 740) {
for (var i = 0; i < anchorIds.length; i++) {

I do not have much experience with JavaScript nor jQuery and I have been struggling with this issue all day.

Thank you!

Answer Source

What happens if you try to use a delegate instead?

$(function() {
    $(document.body).on('click', '.imgClick', showPopup);

If this works, then you should try to find a parent element that always exists for the imgClick elements, that is never replaced, and use that as a selector instead of document.body.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download