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.

Nope
  • 22,147
  • 7
  • 47
  • 72
Severe Torture
  • 319
  • 5
  • 26

1 Answers1

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];
Explosion Pills
  • 188,624
  • 52
  • 326
  • 405