0

I have problem with variables in javascript. My situation is:

In PHP file i have:

<div class="fd_text" onmouseover="zobraz_text('pom','1','1')" onmouseout="zobraz_text('pom','1','0')">something in</div>

In JS file I have:

var pom1 = "Some text1";
var pom2 = "Some text2";

function zobraz_text(firma, cislo, udalost){
    obsah_text = firma+cislo; //this is wrong and why I wrote lower in text under this code

    document.getElementById("bloks").innerHTML = document.getElementById("bloks").innerHTML + obsah_text; //this ID is correct
}

obsah_text is variable which must add texts from pom1, pom2, etc...
Where pom1 and where pom2 I get from mouseover which is in PHP file.

If I group first two parametrs from function zobraz_text I gave pom1, but this pom1 isn´t same as pom1 where I have text. On web I have text "pom1", but I must have text "Some text1".

My code works when I delete variable obsah_text and simply add variable pom1 as in this example code.

This show me text from variable and this is ok but if I add variable then this code works only in 1 of 300 situation (for that I have first and second parametrs in function zobraz_text() )

document.getElementById("bloks").innerHTML = document.getElementById("bloks").innerHTML + pom1;

I believe that you understand and help me. I expect it will be for many of you simple.

0

1 Answer 1

3

You can't create variables of variables. If pom1 and pom2 were global, you could potentially do window[firma + cislo], but I wouldn't recommend that.

Instead, use an object to store the poms:

var poms = {
    "pom1": "Some text1",
    "pom2": "Some text2",
}
//snip
obsah_text = poms[firma + cislo];
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.