Brotzka Brotzka - 5 months ago 13
Javascript Question

JS - Attach string-value to a constructer call

I use the google virtual keyboard in my project. To initialize the keyboard, I run the following script:

google.load("elements", "1", {
packages: "keyboard"
});
function onLoad() {
console.log(lang);
var kbd = new google.elements.keyboard.Keyboard(
[google.elements.keyboard.LayoutCode.GERMAN],
['input1']);
}
google.setOnLoadCallback(onLoad);


But I want to replace
GERMAN
with a individual String. But how is this possible? I have defined a variable
var lang = SWEDISH
and tried the following:

function onLoad() {
console.log(lang);
var kbd = new google.elements.keyboard.Keyboard(
[google.elements.keyboard.LayoutCode.lang],
['input1']);
}


But this results in the following error-message:
uncaught undefined is not a supported layout.


Any Idea, how to solve this problem?

Answer

have you tried:

google.load("elements", "1", {
    packages: "keyboard"
});
function onLoad() {
    console.log(lang);
    var kbd = new google.elements.keyboard.Keyboard(
        [google.elements.keyboard.LayoutCode[lang]],
        ['input1']);
}
google.setOnLoadCallback(onLoad);

more info: Google Developers: Virtual Keyboard