Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert variable into HTML

I'm having a little problem, i have a translations JSON variable. And i want to put it on HTML. Is there a way to make it somehow?

My JS file:

var Karting = Karting || {};

Karting = {

lang : 'lv',
translationsLV: {
    "Home" : "Ziņas",
},
}

And i want to do this:

My page is static and i'm not using any templating engines.

<li><a class="active" href="#">Karting.translationsLV['Home']</a></li>

EDIT:

Added this:

$(window).load( function() {
        var translations;
        if (Karting.lang=='lv')
        {
            translations = Karting.translationsLV;
        }
        else
        {
            translations = Karting.translationsENG;
        }
            }, false );

This doesn't show my element

document.write(translations['Home'])

UncaughtRefferenceError - translations is not defined

like image 589
Cheese Avatar asked Aug 03 '13 12:08

Cheese


1 Answers

One way is to use document.write:

<li><a class="active" href="#"><script>document.write(Karting.translationsLV['Home'])</script></a></li>
like image 111
Ivan Chernykh Avatar answered Sep 24 '22 21:09

Ivan Chernykh