-3

I am building a small achievement list for a website. I'm wondering if there is a possibility to have the list in one js document and then call their respective strings depending on which achievement they've earned. That way I'd have my list in one document and the code in another. How would I go about to create this?

EDIT: Okay, so I'll try to explain a little better with what I have in my mind for this.

<p>Achievement 1 - Some text goes here as well</p>
<p>Achievement 2 - Some text goes here as well</p>
<p>Achievement 3 - Some text goes here as well</p>
<p>Achievement 4 - Some text goes here as well</p>

if(level === 5) {

    $('#achievement2').animate({left:"10px"}, function(){
        $(this).delay(3500).fadeOut(500).promise();
    });

    $('.achievementCont').append(Achievement 1);

}
Yamada Akira
  • 77
  • 1
  • 10
  • Please have a look at the Help pages to learn what makes a good question here at SO. – isherwood Oct 12 '16 at 21:09
  • Maybe this gonna help [http://stackoverflow.com/questions/6734552/newbie-question-connect-two-functions-int-two-files](http://stackoverflow.com/questions/6734552/newbie-question-connect-two-functions-int-two-files). hope it helps. – Lourenço Biselli Oct 12 '16 at 21:11
  • @isherwood I would have provided code of I had a clue to do this. – Yamada Akira Oct 12 '16 at 21:30
  • @Lourenco It doesn't I'm afraid. I would like to have all my achievements in a list in one document and then when they earn one they proper string will be prepended into the achievement container. – Yamada Akira Oct 12 '16 at 21:30
  • Possible duplicate: http://stackoverflow.com/questions/3244361/can-i-access-variables-from-another-file – isherwood Oct 12 '16 at 21:33
  • @isherwood Not a duplicate. – Yamada Akira Oct 12 '16 at 21:36
  • It's hard to say from your vague post. Look at global variables or JSON (for which you'll find a great many existing resources), and come back with a more specific question. – isherwood Oct 12 '16 at 21:37
  • @isherwood Tried to clear it up with what I have in my mind for this. – Yamada Akira Oct 12 '16 at 21:50

1 Answers1

1

If you're okay with making your text more convenient for javascript, maybe convert your achievement texts to an array like this:

window.achievements = [
    "<p>Achievement 1 - Some text goes here as well</p>",
    "<p>Achievement 2 - Some text goes here as well</p>",
    "<p>Achievement 3 - Some text goes here as well</p>",
    "<p>Achievement 4 - Some text goes here as well</p>",
];

Then you could access them with achievements[2] or such. They could be setup in another javascript file. Just be sure you load it ahead of the files that use the data.

Keep in mind that the array is zero indexed so [0] matches up with first achievement. If you'd like the achievement numbers to match up with the index numbers add an extra item (such as none or "") at the beginning of the array. If you do that, be careful when you use for loops against the array.

Ouroborus
  • 12,316
  • 1
  • 27
  • 51