I asked a question a while back about how to get the current variable in a loop and I got the solution :
for (i in ...)
{
...
href:"javascript:on_click('+i+');"...}
When i run this, the loop is sending the on_click function the string 'i' instead of the value of i.
Am I using the +variable+ wrong? Can someone explain in more detail what wrapping a variable in + means and why it is not working in my case?
4 Answers 4
Yes, you are - if you start a string with ", you have to end it with " (and vice versa for '), too. The syntax highlighting here at SO demonstrates this quite well too.
href:"javascript:on_click("+i+");"...}
(What is happening is that ' within a string surrounded by " is treated as regular ' character, it does not start nor end a string literal here).
2 Comments
+i+? I'd like to look up more information on it except I don't know what to search upi is converted to a string and concatenated to the surrounding strings.Try this:
href:"javascript:on_click('" + i + "');"
2 Comments
Appears that you are missing double quotes to close your string. Javascript doesn't do variable interpolation in a quoted string.
Try:
href:"javascript:on_click('" + i + "');"...}
Comments
You're mixing your quote marks.
It should be:
href: "javascript:on_click('" + i + "');"
Note how the double quotes are necessary at both ends of the invariant parts.