# Other Languages > XML, HTML, Javascript, Web and CSS >  [RESOLVED] Wierd issue in javascript

## DataMiser

I have ran into an issue this morning that has me a bit dumbfounded.
below is a portion of the line that is causing my headaches


```
<td><button class='smallbutton'  onclick='ReleaseStation("+station+");'>"+istatus+station+"</button></td>
```

There are several variables available here one of which is named clid
The ReleaseStation function expects the clid to be passed but for whatever reason when I write it this way nothing happens


```
<td><button class='smallbutton'  onclick='ReleaseStation("+clid+");'>"+istatus+station+"</button></td>
```

the clid occurs again in another column and is displayed as expected.

I have also tried replacing the call to ReleaseStation with just an alertbox and get the same result which is if I have 
alert("Test");  it works
alert("+station+"); it works
alert("+clid+"); nothing happens.

I have no idea what is going on here. To me that should be working but it is not.

Any ideas?

----------


## DataMiser

btw both station and clid are strings and both contain a value
Also if I place the literal value of clid in there it works but fails to execute the function when the var clid is used????

----------


## jpbro

What's the value of "clid"? Could there be any characters (like ', ") that need escaping?

----------


## DataMiser

here is an example of the content in the clid var
0121_0356_14X72_2

If I use the literal value it works but when using the variable that holds this value it does not appear to call the function at all.

----------


## jpbro

Hmm, nothing weird about that content that I can see.

If you go to the Console page in your browser's Developer's Tools, what errors (if any) appear when you click the button?

----------


## jpbro

Also, I'm assuming that the HTML snippets you posted are actually inside JS strings, and not raw HTML? Because if that is raw HTML then your "+clid+" is inside a string and won't be evaluated as a variable.

----------


## techgnome

I'd change this:


```
<td><button class='smallbutton'  onclick='ReleaseStation("+clid+");'>"+istatus+station+"</button></td>
```

To this:


```
<td><button class='smallbutton'  onclick='ReleaseStation("+clid+");'>"+clid+"</button></td>
```

And display the clid value in the button, just to make sure it is what you think it is at the time it should be ... 
If it's working with station, there's no reason to think it wouldn't work with clid, unless clid isn't what you think it is.

-tg

----------


## DataMiser

Yes that is within a larger line of javascript. as written in the first snippet it works as expected, the second one does not. The only difference is the variable in use.

The clid is written in the next column of the table. normally it is hidden but I did make it visible just to be sure it was what I thought. It looks exactly as expected. I copied the example value above directly from the table. I looked at the array that comes from the server which is where the clid is generated and there too it looks exactly as expected. I even tried pasting in the literal value and that worked but using the variable has failed everytime.

I'll take another look at the console display  and see what I can find there.

----------


## DataMiser

Well, as a last ditch effort I tried using quotes around the variable and it looks as though it might have worked. No error and the value of clid is carried into the function. I will have to go a little deeper, restore the function and see if it executes properly.

----------


## DataMiser

So it looks like I got it working though am still confused as to why this was needed.



```
<td><button class='smallbutton' onclick=\"ReleaseStation('"+clid+"');\">"+istatus+station+"</button></td>
```

I guess it was work with station because even though it was a string it contain a numeric value and clid was alpha numeric.

I do not do much browser based stuff and had not thought to look under the developers tools for some kind of error message doing that helped me track it down.

----------


## jpbro

Maybe because the clid started with a number? Not sure if JS will try to automatically convert it to a number unless you force it to be a string?

----------


## jpbro

A yes, here's the error in the console:



```
alert(0121_0356_14X72_2);
```




> Uncaught SyntaxError: numeric separators '_' are not allowed in numbers that start with '0'

----------


## jpbro

Apparently JS allows underscore as a separator for numeric literals to make them easier to read in code. e.g.: 10000000 can be written as 10_000_000

Guess the lessons learned here are:

1) Always use quotes around variables that you want to use as text - don't rely on JS to convert them to strings for you.
2) Check your console when things aren't working as expected - error messages appear there that can point you in the right direction.

----------


## DataMiser

The odd thing is that the clid was sent down as a string, showed in the console on the return from the server as a string and in some cases had alpha characters in it but still failed unless the quotes were added around it for the function call. Kind of like when you are passing a string variable into sql query. I feel a bit stupid that it took me that long to find it but I really never would have guessed that would be the issue here.

One thing for sure, I'll remember this, the console and will be more careful next time.

----------


## techgnome

It's the difference between this:


```
alert(100);l
```

and this:


```
alert('this is a string');
```

This will break:


```
alert(This is a string);
```

which is essentially what you were trying to do ... 

This is one of the things I hate about JS... it's loosey goosey treating with types ... 


-tg

----------


## DataMiser

yep apparently it was not treating the string  the way I expected it to

Var mystring="this is a test";

alert(myString);

You would expect it to be treated as
alert("this is a test");
but apparently it was seeing it as 
alert(this is a test); 
and failing

Whats worse is in some cases it was working so yeah. I definitely do not like that aspect of it.

----------


## techgnome

but you didn't have alert(myString) ... you had alert("+clid+") ... the clid isn't part of the javascript ... it's value is ... so you need to enclose its VALUE in quotes to have it treated like a string.

-tg

----------


## yereverluvinuncleber

I do hate those difficult issues that arise when you are fighting against javascript's loose typing. Thumbs up to the solution JB. 
If .js has ONE flaw then it is inadequate typing.

----------


## dday9

> I do hate those difficult issues that arise when you are fighting against javascript's loose typing. Thumbs up to the solution JB. 
> If .js has ONE flaw then it is inadequate typing.


This is off topic, but typescript is basically ES6 JavaScript with typing.

----------


## DataMiser

I'm just glad I managed to find the solution. Such a silly mistake that had me spinning my wheels.
Thanks to jbpro for pointing out the console in developer tools. I had been testing in the default ie and not seeing any messages.
Switched to chrome and opened the console, not only helped with that issue but any that followed.
I will definitely be doing it that way from now on.

In this project I have been working on a remote system and all the coding has been free hand in notepad++

Coding in a text editor really makes you appreciate the power of a good IDE.

----------

