-2

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.getElementById("second").value; //value == bye
 var third = document.getElementById("third").value;   //value == crazy
 var forth = document.getElementById("forth").value;    // value == night


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

I want the string to display:

 <script> 
 var firstValue = " hello "; 
 var secondValue = "  bye "; 
 var thirdValue = " crazy "; 
 var forthValue = " night "; 
 </script> ";
2
  • 1
    You're question is not clear. What are you trying to do exactly? Why do you need to do this? Commented Jan 17, 2014 at 17:48
  • 1
    There are multiple issues with your question that are causing answerers to have to guess how to help you. Could you share a program that doesn't have any syntax errors with us, perhaps in the form of a Minimum Complete Valid Example Commented Jan 17, 2014 at 17:51

4 Answers 4

2

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

So change your variable names.

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

should be better.

Sign up to request clarification or add additional context in comments.

Comments

0

Just use the

`
var string = `<script> 
 var firstValue = " hello "; 
 var secondValue = "  bye "; 
 var thirdValue = " crazy "; 
 var forthValue = " night "; 
 </script> ";`

Comments

-1

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

See this fiddle

2 Comments

function myFunction() { var amen1 = document.getElementById("amen1").value; alert(amen1.toString().split(" ")); }. This will also work
@VinayakPingale i know yes it will work.He already mentioned that in his question
-1

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!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.