Skip to content Skip to sidebar Skip to footer

How To Use A Variable Inside A String

I can take value of a span with this code: document.getElementById('aaa:j_idt89:0:asd').innerHTML but I want to use variable like i=1, for example like this: i=1 document.getElem

Solution 1:

Use this code

document.getElementById("aaa:j_idt89:"+ i +":asd").innerHTML

Note the change I made inside. "+ i +" . You actually needed a String Concatenation.

So explaining the code.

when i = 1

"aaa:j_idt89:"+ i +":asd" = "aaa:j_idt89:"+ 1 +":asd" = "aaa:j_idt89:1:asd" = thats what you need

Solution 2:

This should work for you:

var i = 1;
var element = document.getElementById("aaa:j_idt89:" + i + ":asd").innerHTML

you need to build your string up and to use your variable you need to concatenate it to your string like above " + i + " and it will work.

Post a Comment for "How To Use A Variable Inside A String"