Skip to content Skip to sidebar Skip to footer

Javascript Displaying A Var In Code

I want the value of some vars to be shown in the string. function test() { var first = document.getElementById('first').value; //value ==hello var second = document.getElementB

Solution 1:

variables names in javascript can't start with a number.

So change your variable names.

var name1  = document.getElementById('1').value;

should be better.

Solution 2:

To convert any value to string just concatinate value with an empty string like - ""+document.getElementById('1').value

See this fiddle

Solution 3:

I think your problem just boils down to wrong escaping in the string, which you should recognise simply by code highlighting:

var myString =" \
 <script> \
 var firstValue = \""+ first +"\"; \
 var secondValue = \""+ second +"\"; \
 var thirdValue = \""+third +"\"; \
 var forthValue = \""+forth +"\"; \
 <\/script> ";

You always escape the characters you want to keep in your string and not those, which have a syntactical meaning in the definition!

Post a Comment for "Javascript Displaying A Var In Code"