1

I have this code to generate n amount of divs with id based on their x and y position:

    $("#gameContent").append($("<div></div>").attr("id", "board"));
    var card;
    for (var y = 0; y < clientBoard.length; y++) {
     for (var x = 0; x < clientBoard.length; x++) {
      if(clientBoard[y][x] == undefined){
       continue;
      }
      card = $("<div></div>").addClass("card");
      card.attr("id", (y + ":" + x));
      card.html("Value " + clientBoard[y][x].value);
      if (x == 0) {
       card.addClass("clearleft");
      }
      $("#board").append(card);
     }
    }

This works and when i inspect in chrome all divs have their correct (e.g "1:2"). Then I want to use the HTML() function to change the content of those divs. But when using:

    console.log($("#" + y + ":" + x).html());

It prints undefined

I will use this code to update the html content for the divs:

    for (var y = 0; y < clientBoard.length; y++) {
     for (var x = 0; x < clientBoard.length; x++) {
      if(clientBoard[y][x] == undefined){
       continue;
      }
      $("#" + y + ":" + x).html("Value: " + clientBoard[y][x].value);
     }
    }

I have tried to google and look at the documentation of the html(), but can't seem to find anything that is wrong. I used the html() earlier in my code, only difference is the selector there was just a string and not a combination of string and a variable. That worked great.

I also printed all the selectors it tried to use and compared them with the id of the divs, and they are correct.

I have also tested and printed the value that it tries to put into the divs, that also worked

Ask if I should provide more information.

3
  • 1
    I'm surprised you don't get an error, as : is a special character and will need to be escaped in the selector. Commented Oct 10, 2018 at 8:54
  • Thank you. For some reason it didn't give me an error. I knew of some special characters, but obviously not all. Commented Oct 10, 2018 at 9:01
  • Hi, did you try making sure x and y are string instead while you using selector? Commented Oct 10, 2018 at 9:03

1 Answer 1

1

In order to use an id with a colon in the selector you will need to escape it:

$('#1\\:2');
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.