0

I have created a javascript that runs when the user change the dropdown list (Note: the change function, and the way we get the value of the dropdown list work fine), I have tested the function multiple times and found the issue is my second For-loop I am really stuck wondering why it does not work.

$(document).ready(function(){
        var username = prompt("Please enter your Username", "default");
        console.log("User: ",username);

        $.ajax({ url : "/memory/intro",
                type: "POST",
                data: JSON.stringify({"username": username}),
                contextType: "text/json; charset=utf-8",
                success : function(){console.log("Done!");}});

        $("#lvl").change( function (){
                var diff = $(this).find("option:selected").attr('value');
                console.log(diff); //Personal testing
                console.log(typeof(diff)); //Personal testing
                for(var i=0; i < diff; ++i){
                        var row;
                        row = $("<tr></tr>");
                        for(var x=0; i < diff; ++x) {
                                var col;
                                col = $("<div></div>");
                                col.addClass("card");
                                col.data("col", x);
                                row.append(col);
                        }
                        $("#cards").append(row);
                };

        });

});

Note: If we comment the second for-loop the whole code works fine and we append the "tr" and "div" with the class name

4
  • 1
    for(var x=0; i < diff; ++x) { should maybe be for(var x=0; x < diff; ++x) { (i.e. the stop condition depends on x, rather than i) Commented Mar 6, 2017 at 20:07
  • In your second loop you use i < diff;, shouldn't it be with an x x < diff; Commented Mar 6, 2017 at 20:08
  • If this is the case, where change to x fix it, please consider delete this post as it is a simple typo issue Commented Mar 6, 2017 at 20:09
  • Thanks @nbrooks the code seemed so fine, I think I need glasses Commented Mar 6, 2017 at 20:12

2 Answers 2

1

Sadly I didn't realise that I had i < diff on the second loop, thanks to @nbrooks

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

Comments

0

second forloop try:

      for(var x=0; x < diff; ++x) {
            var col;
            col = $("<td></td>");
            col.addClass("card");
            col.data("col", x);
            row.append(col);
      }

So far as i know you shouldn't append a div to a tr, it causes errors (not js errors, but it are still markup errors)

check w3c validator if you want to be sure of this issue,

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.