Yamada Akira Yamada Akira - 1 month ago 6
HTML Question

Get paragraphs from a js file and insert them into a div in another js file

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);

}

Answer

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.