0

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)?

6
  • "<div class='uk-width-medium-1-4'> <div class='md-card'> <div class='md-card-head uk-text-center uk-position-relative'>.... is not required for your question. Read How to Ask. Pay close attention to MCVE. Correct your question. Commented Dec 30, 2015 at 21:53
  • @Amit It was relevant in so much as there might have been something else at play that I wasn't aware of, so I was giving as much info as I felt relevant. I will remove it as I have now clarified the query as per HelloWorld comments, but at the time it could have been wholly relevant. Commented Dec 30, 2015 at 22:12
  • No. it was never relevant. Not because "in the end it turned out not to be", but because had you cared to "ask a good question" as explained in How to Ask, you'd take the effort to isolate your problem and remove boilerplate. Commented Dec 30, 2015 at 22:17
  • And you're edit, although welcome and improves your question is still quite far from minimal. Commented Dec 30, 2015 at 22:19
  • @Amit what else do you want to remove? Commented Dec 30, 2015 at 22:28

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.