chaitanya dalvi
chaitanya dalvi

Reputation: 1669

Double quote gets replaced by &quote;

My JavaScript variable contains a string:

{"start":{"lat":19.0759842,"lng":72.87765630000001},"end":{"lat":18.5206624,"lng":73.8567415},"waypoints":[[18.8753235,73.52948409999999]]}

But when I show it into HTML component, it looks like:

{"start":{"lat":19.0759842,"lng":72.87765630000001},"end":{"lat":18.5206624,"lng":73.8567415},"waypoints":[[18.8753235,73.52948409999999]]}

Upvotes: 4

Views: 10796

Answers (4)

hmairi halim
hmairi halim

Reputation: 11

if you are try dispay json data in view blade in laravel try to do this

<php echo $data ?>

or

@php echo $data; @endphp instead of {{ $data }}

Upvotes: 0

Muhammad Suleman
Muhammad Suleman

Reputation: 2932

var text1 = '{&quot;start&quot;:{&quot;lat&quot;:19.0759842,&quot;lng&quot;:72.87765630000001},&quot;end&quot;:{&quot;lat&quot;:18.5206624,&quot;lng&quot;:73.8567415},&quot;waypoints&quot;:[[18.8753235,73.52948409999999]]}';
var text2 = text1.replace(/&quot;/g, '\"');

alert('Your data\n' + text1);

alert('Required data\n' + text2);

Upvotes: 2

Drew Gaynor
Drew Gaynor

Reputation: 8482

Output the string as HTML instead of plain text so the HTML entity &quot; is rendered correctly.

var s = "{&quot;start&quot;:{&quot;lat&quot;:19.0759842,&quot;lng&quot;:72.87765630000001},&quot;end&quot;:{&quot;lat&quot;:18.5206624,&quot;lng&quot;:73.8567415},&quot;waypoints&quot;:[[18.8753235,73.52948409999999]]}";

//Incorrect with jQuery
$("#incorrect-jquery").text(s);

//Correct with jQuery
$("#correct-jquery").html(s);

//Incorrect with plain JavaScript
document.getElementById("incorrect-js").textContent = s;

//Correct with plain JavaScript
document.getElementById("correct-js").innerHTML = s;
div {
  margin-bottom: 20px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<strong>Incorrect with jQuery</strong>
<div id="incorrect-jquery"></div>

<strong>Correct with jQuery</strong>
<div id="correct-jquery"></div>

<strong>Incorrect with plain JavaScript</strong>
<div id="incorrect-js"></div>

<strong>Correct with plain JavaScript</strong>
<div id="correct-js"></div>

Upvotes: 5

Parasmani Batra
Parasmani Batra

Reputation: 207

use decodeURI javascript function to show example

var s= decodeURI('your string');

Upvotes: -1

Related Questions