Clarification:
I really don't want to embed my
js
in myhtml
. The suggestions I'm getting, like, this or that post might contain the possible answer, well in those answers, they embedded theirjs
inhtml
. Both myjs
andhtml
is large. So embedding will surely make them messy and hard to debug
In my project there's a module where user can see details about product and add products to cart. For this he needs to specify the number of products he wants to add. It looks like
For this I have a +
and -
icon. I have a js
file where it increments or decrements each time +
or -
has been clicked.
Now, what I want is this:
A user can add products but if the total number exceeds the number of products stored in the database, he can't add products even if he click the
+
button. For example, if there are 4 items and he clicked 5 times then on the 5th click the amount will remain 4, won't increase.
So I need a django variable in my js
file. I looked up how to do that, and I found some solutions.
Some solution says var data = '{{data|escapejs}}'
this while some solution says
<script>
var myTable = {% autoescape off %}{{ table_data }}{% endautoescape %};
</script>
But I don't want to embed js
on my html
like this. Here is my js
file:
function incrementValue()
{
var value = parseInt(document.getElementById('quantity').value, 10);
value = isNaN(value) ? 0 : value;
value++;
document.getElementById('quantity').value = value;
}
And my html
, from where I am calling this js
like this:
<span class="btn btn-default btn-lg btn-qty" onclick="incrementValue()">
<span class="glyphicon glyphicon-plus" aria-hidden="true">
</span>
</span>
<input class="btn btn-default btn-lg btn-qty" id="quantity" value="0" />
<span class="btn btn-default btn-lg btn-qty" onclick="decrementValue()">
<span class="glyphicon glyphicon-minus" aria-hidden="true"></span>
</span>
</span>
What is the best way to pass django
variable to js
or, How should I pass the django
variable to my js
so that there will be no vulnerability or no security issues and also I don't want to embed js
in my html
.