-1

I need to append the vidyourl3, but my script keeps adding vidyourl3 name only. Where am I doing a mistake ?

var vidyourl3 = '<iframe id="vide3" width="255" height="195" 
src="http://www.youtube.com/embed/Nficz8WZwrk?wmode=opaque" frameborder="0" 
allowfullscreen></iframe>';

kacki = 3;

$("#vidgelcekburaya").html("vidyourl"+kacki);
user3304007
  • 446
  • 1
  • 7
  • 17

3 Answers3

1

You're putting a literal string in your html. Take the quotes off of your vidyourl and it will work:

$("#vidgelcekburaya").html(vidyourl3 + kacki);

Another problem which is hard to decide what you're trying to do is; your first variable is called vidyourl3, while you then put in vidyourl in your html replace. Which one should it be?

ntgCleaner
  • 5,865
  • 9
  • 48
  • 86
  • 1
    sorry again I think what the OP is trying to do is to create the variable name dynamically like `"vidyourl"+kacki` to access the content of the variable named `vidyourl3`... you are not doing the same – Arun P Johny Feb 13 '14 at 00:07
  • Sure, but he's not doing the same by naming his FIRST variable vidyourl3. What he would be writing is vidyourl33 There's no variable called vidyourl. He can make `vidyourl` as a first variable, then number all the variables he wants, then use `kacki` to number them, either way, when you put quotes around a variables, it turns into a string. – ntgCleaner Feb 13 '14 at 00:09
  • @ntgCleaner No. Notice he left off the `3` in the literal string => `"vidyourl"`... – War10ck Feb 13 '14 at 00:10
  • I see, it wasn't apparent that it was on purpose – ntgCleaner Feb 13 '14 at 00:13
1

If you have multiple urls then create an object to hold those references as given below

var urls = {
    vidyourl3: '<iframe id="vide3" width="255" height="195" 
src="http://www.youtube.com/embed/Nficz8WZwrk?wmode=opaque" frameborder="0" 
allowfullscreen></iframe>'
}

then use the object key along with bracket notation to access the value

kacki = 3;
$("#vidgelcekburaya").html(urls["vidyourl"+kacki]);
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
0

Try:

window.vidyourl3 = '<iframe id="vide3" width="255" height="195" 
src="http://www.youtube.com/embed/Nficz8WZwrk?wmode=opaque" frameborder="0" 
allowfullscreen></iframe>';

kacki = 3;

$("#vidgelcekburaya").html(window["vidyourl"+kacki]);

If your variable needs to be in the local scope, try

var vidyourl3 = '<iframe id="vide3" width="255" height="195" 
src="http://www.youtube.com/embed/Nficz8WZwrk?wmode=opaque" frameborder="0" 
allowfullscreen></iframe>';

kacki = 3;

$("#vidgelcekburaya").html(eval("vidyourl"+kacki));

I wouldn't use this method, because eval can be kind of scary.

Cilan
  • 13,101
  • 3
  • 34
  • 51