i'm new in JS, i have a 3x3 table filled as below:
<table id="mytable">
<tr>
<td id="1" onclick="select_home(1)"></td>
<td id="2" onclick="select_home(this.id)"></td>
<td id="3" onclick="select_home(this.id)"></td>
</tr>
<tr>
<td id="4" onclick="select_home(this.id)"></td>
<td id="5" onclick="select_home(this.id)"></td>
<td id="6" onclick="select_home(this.id)"></td>
</tr>+
<tr>
<td id="7" onclick="select_home(this.id)"></td>
<td id="8" onclick="select_home(this.id)"></td>
<td id="9" onclick="select_home(this.id)"></td>
</tr>
</table>
im updating template whenever a click happen on tables as below:
var table = document.querySelector("table");
// listen for a click
table.addEventListener("click", function (ev) {
// get the event targets ID
var serviceID = ev.target.id;
turn() ? blue_checked(serviceID) : red_checked(serviceID);
});
function blue_checked(id) {
var table = document.getElementById("mytable").getElementsByTagName("td");
selected_home_blue.push(id);
table[id - 1].style.backgroundColor = "blue";
check_game(selected_home_blue, "blue");
}
i am updating background color of td's first then run check_game function but check_game run first and then td background color change, how can i handle it ?
check_gamerun first?idas a string, but then you try to subtract one from the id withtable[id-1]. If you want to treat theidas a number you could useparseInt(id). By the way, your code snippet misses a lot of code so it's kind of hard for us to reproduce your problem.