RonnieT
RonnieT

Reputation: 2203

Escape Double Quotes JQuery

Trying to place a variable into the value field as shown below:

<input type="hidden" id="descrip" value="' + descrip + '" name="descrip[]">

However if working with a string such as 1" RT OFFSET with double quotes it places things into value as this:

<input type="hidden" id="descrip" value="1" rt offset" name="descrip[]">

Since this places value as value="1" it stops at the 1 and ignores the rest.

Thinking I would have to escape the double quotes I tried the below, but that only sets the value to value="1\" at that point so still cuts off at the " as in 1 inch.

descrip = descrip.replace(/(["])/g, "\\$1");

What am I missing here to set the value field of descrip properly when involving a double quote?

Upvotes: 0

Views: 3017

Answers (1)

anhlc
anhlc

Reputation: 14429

Try this

descrip = descrip.replace('"', '&quot;');

When you post it back to server, replace it again.

Upvotes: 3

Related Questions