views:

45

answers:

2
var input;

// method 1
input = document.getElementById('address').value;
alert(input)

// method 2
eval('input = "'+document.getElementById('address').value+'"')
alert(input)

method 1 is working fine, but method 2 is not working when newline characters are inputted and it says "unterminated string literal".

I need to store values using eval anyway. So please help me.

+1  A: 

That's because you are actually evaluating code that looks like this, which is ofcourse wrong in javascript

​var test = "Lorem ipsum
    dolor sit amet"; ​​​​​​​​​​​​​​​​​

Why do you need the eval? It looks not really necessary in this case. Maybe we can help you get around the eval?

ChrisR
+1  A: 

Use string.replace(/\r?\n/g, "\\n") to escape the newlines.

Aaron Digulla
thanks buddy, it works great!!!
Saiful