I have the following snippet of code which gives me unterminated string literal error
$(function() {
$('#addDropdown').click(function() {
var $d = $('{{ form |bootstrap }}').fadeIn().delay(1000);
$('#dropdownContainer').append($d);
});
});
I have looked unterminated string literal question and found that it is because multiline problem.My {{ form |bootstrap }} has multiple lines of code as mentioned in the question.So how do I handle the problem
To solve the "Unterminated string constant" error, make sure to enclose your strings in quotes consistently. String literals must be enclosed in single quotes, double quotes or backticks. When writing a multiline string use backticks.
This JavaScript error unterminated string literal occurs if there is string which is not terminated properly. String literals must be enclosed by single (') or double (“) quotes.
In general, to use with JavaScript, you should use |escapejs
filter:
var $d = $('{{ value|escapejs }}');
However, as a matter of best practice you'd better put {{ form|bootstrap }}
in HTML, and use JavaScript just to fade in:
<div id="djangoForm" style="display:none">{{ form|bootstrap }}</div>
<script>
$('#addDropdown').click(function() {
$('#dropdownContainer').append($('#djangoForm'));
}
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With