Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot set val when string in double quotes

I have the following variable -

termText = $("#termBox").val();

where $("#termBox").val() is equal to - "\"Test\""

I then am creating an html object and want to set its value to termText like -

<input type="text" id="secondBox" value="' + termText + '">

However this is not working. The odd thing is when $("#termBox").val() does not have double quotes in it i.e. "Test" (in javascript debugger is adding the quotes to show it is a string), this works fine and populates secondBox value perfectly.

I dont want to escape double quotes from the string as they are required as there has to be a definition between when a user enters "Term" or Term however this is not working for double quotes.

How can I do this?

like image 774
Ebikeneser Avatar asked May 21 '26 08:05

Ebikeneser


1 Answers

You have to escape it with html equivalent when you create input.

<input type="text" id="secondBox" value="' + termText.replace(/\"/g,"&quot;") + '">
like image 173
Emre Senturk Avatar answered May 22 '26 22:05

Emre Senturk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!