Rajat Saxena
Rajat Saxena

Reputation: 3925

How to substitute string coming from django in javascript?

Suppose this is a part of my django template:

<div class="txt"></div>
<script>
    $(document).ready(function(){
    var text = '{{ each.text }}'
    $('.txt').html(text);
    });
</script>

So this code is messing my output.Suppose the string returned by django to this template is This is great'thing.So here the output is only This is great.So how can I escape quotes and blockquotes in javascript?

Upvotes: 0

Views: 98

Answers (2)

Rohan
Rohan

Reputation: 53366

Check how to escape html text in template. Refer template filter escape

Upvotes: 0

Ry-
Ry-

Reputation: 225054

Try the escapejs filter:

var text = '{{ each.text | escapejs }}';

Upvotes: 1

Related Questions