• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1815
  • Last Modified:

Javascript: Using eval() with a string containing double quotes and single quotes

Hello all.

I am trying to "push" values into an array after they've been used in an eval() statement. The issue I'm running into is that with the way I'm concatenating the string, the double quotes inside the "text being evalled" is causing the eval() function to choke.

Here is an example.

Description fields is equal to -------->        This is a test, "yes", a test

eval("obj." + "description" + " = '" + $('#description').val() + "'");      

With this as an example, the " in "yes" messes things up. I've tried using " but the & then messes it up. I've also tried replacing the " with \".

Any suggestions on how to "escape" the double quote?
0
Webspeeder
Asked:
Webspeeder
1 Solution
 
SANDY_SKCommented:
have you tried replacing " with \\\" OR \\\\\"

That is escaping the the escaping character
0
 
Rajar AhmedConsultantCommented:
instead of
eval("obj." + "description" + " = '" + $('#description').val() + "'");
have u tried like below
eval("obj." + "description" + " = '" + $('#description').val().replace('"','\"') + "'");
0
 
WebspeederAuthor Commented:
replacing " with \" generates this JS error in firebug.

Error: missing ; before statement
Source File: http://10.9.10.220/js/jquery2/jquery-1.6.2.min.js
Line: 38, Column: 22
Source Code:
obj.description = '9' 6" 2PC 3LBL testing 3'

Well, looking at the message closely, why would this be tied to jQuery?

var xx = htmlEncode($('#description').val());
eval("obj." + "description" + " = '" + xx + "'");

function htmlEncode(value){
  return value.replace(/"/g,'\"');
}
0
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
WebspeederAuthor Commented:
When trying to "escape the escape character", I get this:

Error: missing ; before statement
Source File: http://10.9.10.220/js/jquery2/jquery-1.6.2.min.js
Line: 38, Column: 22
Source Code:
obj.description = '9' 6\\" 2PC 3LBL testing 3'
0
 
WebspeederAuthor Commented:
Might it be the single quote messing it up?
0
 
WebspeederAuthor Commented:
I've tried escaping both with \' and \"
0
 
SANDY_SKCommented:
can you paste that portion of the code if you don't mind.
0
 
Albert Van HalenAnalyst developerCommented:
Just use
obj["description"] = $("#description").val();

Open in new window

0
 
WebspeederAuthor Commented:
That did it. Thank you very much.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now