0

I am quite new to javacript and I am trying to add a numerical counter as a loop to the below function to save me typing it out 8 times!

The Function needs to be called loadPopup1 - loadPopup8 and the #toPopup div needs to be toPopup1 - toPopup8.

Here's my code:

function loadPopup() { 
    if(popupStatus == 0) { // if value is 0, show popup
        closeloading(); // fadeout loading
        $("#toPopup").fadeIn(0500); // fadein popup div
        $("#backgroundPopup").css("opacity", "0.7"); // css opacity, supports IE7, IE8
        $("#backgroundPopup").fadeIn(0001); 
        popupStatus = 1; // and set value to 1
    }   
}

Many thanks for your help!

Pete

3 Answers 3

2

Hand over a parameter to the function...

function loadPopup(index) {
   ...
   $("#toPopup" + index).fadeIn(0500);
   ...
}

Then, if you want to act on #1 and #7, call it passing the index...

loadPopup(1);
loadPopup(7);
Sign up to request clarification or add additional context in comments.

Comments

0

this code will create your 8 functions:

for (var i = 1; i <= 8; ++i) {
    (function(i) {
        window['loadPopup' + i] = function loadPopup() {
            if(popupStatus == 0) { // if value is 0, show popup
                closeloading(); // fadeout loading
                $("#toPopup" + i).fadeIn(0500); // fadein popup div
                $("#backgroundPopup").css("opacity", "0.7"); // css opacity, supports IE7, IE8
                $("#backgroundPopup").fadeIn(0001); 
                popupStatus = 1; // and set value to 1
            }
        };
    })(i);
}

Comments

0
function loadPopup(i) {
    if(popupStatus == 0) { // if value is 0, show popup
        closeloading(); // fadeout loading
        $("#toPopup" + i).fadeIn(0500); // fadein popup div
        $("#backgroundPopup").css("opacity", "0.7"); // css opacity, supports IE7, IE8
        $("#backgroundPopup").fadeIn(0001);
        popupStatus = 1; // and set value to 1
    }
}

var wrapper={};
for (var i =0; i < 8; i++){
    wrapper['loadPopup' + i] = new function(){loadPopup(i);};
}

// so you can call
wrapper.loadPopup3();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.