Reputation: 1000
I am using a for loop & jQuery output to html(). to output a JSON response to a html div.
In the for loop, I am transforming each result as follows into the output variable:
$.ajax(settings).done(function(response) {
var output = "";
for (i in response.Products) {
var productID = response.Products[i].ProductId;
var OtherVariables = response.Products[i].OtherResponseFields;
output += "<Some Other Code> <script type=\"application/javascript\"> ...some code unique to each productID... </script>";
}
$("#allResults").html(output);
});
The output works fine when I do not have the following in the output variable:
<script type=\"application/javascript\"> ...some code unique to each productID... </script>
But it breaks when I do add it as per the top code sample. I am looking at using the variable "clicks_productID" for the user to select a product quantity in a popup, before adding the product & qty to my database (not yet built).
Is there a better way of doing this (the results need to be different for each JSON return - which can be a variable response qty based on the submission)?
Upvotes: 0
Views: 128
Reputation: 2330
change
<script type=\"application/javascript\">var clicks_" + productID + " = 1;var minimum = 1;function updateClickCount_" + productID + "() {if (clicks >= minimum) {document.getElementById('clickCount_" + productID + "').innerHTML = clicks_" + productID + ";} else {clicks_" + productID + " = 1;document.getElementById('clickCount_" + productID + "').innerHTML = clicks_" + productID + ";}}</script>
to
<scr" + "ipt type=\"application/javascript\">var clicks_" + productID + " = 1;var minimum = 1;function updateClickCount_" + productID + "() {if (clicks >= minimum) {document.getElementById('clickCount_" + productID + "').innerHTML = clicks_" + productID + ";} else {clicks_" + productID + " = 1;document.getElementById('clickCount_" + productID + "').innerHTML = clicks_" + productID + ";}}</scr" + "ipt>
otherwise the interpreter will think the closing script tag in your string is the end of your script
Upvotes: 1