function BytBild2(number) {
test = "bild" + number.toString();
document.getElementById("storbild").src="bilder/bilder_stora/" + test + ".jpg";
This dosen't work! But if I write bild2, then it works! How come it dosen't work and what do I do wrong? What I'm trying to do is to concatenating different parts to a variable. bild2 ha a value from the beginning. And I just wanted to save a lot of code to put bild + number and use just one line of code instead of several?!
Thanks! But are you sure it works? I have tried this several times! I'm unsure if it's possible to create a new variable named 'test' by concatenating bild + 1 which already is a variable name, 'bild1', with a value of 0011_1. I can't get this value to the new variable 'test'!?
I call it like BytBild2(2); but as I wrote variables 'bild1' to 'bild5' are already declared variables with different values before I call BytBild2(2);
My thought was to just use less amount of lines of code and create a new variable instead of
if (number == 1) {
document.getElementById("storbild").src="bilder/bilder_stora/" +bild1+ ".jpg";} bild1 could have the value of 0004_2
......
if (number == 5) {
document.getElementById("storbild").src="bilder/bilder_stora/" +bild5+ ".jpg";} bild5 could have the value of 0015_5
What I wanted was just this few lines:
function BytBild2(3) {
test = ???? + number
document.getElementById("storbild").src="bilder/bilder_stora/" + test + ".jpg";
}
which already is a variable name, 'bild1', with a value of 0011_1. I can't get this value to the new variable 'test'!?
Like this?
Code:
function BytBild2(number) {
var test = window["bild" + number];
document.getElementById("storbild").src="bilder/bilder_stora/" + test + ".jpg";
}
var bild1 = '0011_1';
BytBild2(1);
At least 98% of internet users' DNA is identical to that of chimpanzees
Bookmarks