I have a table of results retrieved from a MYSQl database with each TR having a unique ID and Class. When clicking on the TD in a row, the script toggles through various colors depending on the order status, and then writes on each toggle back to the DB using AJAX. The problem I am having is that the color changes the last table row td and not the current clicked row td. I am a noob to JS and Jquery and have read that the problem may be "hoisting" which has had me confused for a few days now. Any help is greatly appreciated.
<td id="<?php echo $row[ 'order_id' ]; ?>" class="<?php echo $row[ 'order_id' ]; ?> white"
onclick="changeBackground(this.id)"><?php echo $row[ 'order_id' ]; ?></td>
<script>
function getSelected() {
return document.getElementsByClassName(<?php echo $row[ 'order_id' ]; ?>);
}
function changeBackground(id) {
var all = getSelected();
var x = id
alert(x);
for (var i = 0; i < all.length; i++) {
var color = all[i].classList;
if (color.contains("white")) {
all[i].classList.add("red");
all[i].classList.remove("white");
$.ajax({
type: "POST",
url: 'update_color.php',
data: {color: 2},
success: function (data) {
console.log(data);
},
});
} else if (color.contains("red")) {
all[i].classList.add("green");
all[i].classList.remove("red");
$.ajax({
type: "POST",
url: 'update_color.php',
data: {color: 3},
success: function (data) {
console.log(data);
},
});
} else if (color.contains("green")) {
all[i].classList.add("pink");
all[i].classList.remove("green");
$.ajax({
type: "POST",
url: 'update_color.php',
data: {color: 4},
success: function (data) {
console.log(data);
},
});
} else if (color.contains("pink")) {
all[i].classList.add("yellow");
all[i].classList.remove("pink");
$.ajax({
type: "POST",
url: 'update_color.php',
data: {color: 5},
success: function (data) {
console.log(data);
},
});
} else if (color.contains("yellow")) {
all[i].classList.add("white");
all[i].classList.remove("yellow");
$.ajax({
type: "POST",
url: 'update_color.php',
data: {color: 1},
success: function (data) {
console.log(data);
},
});
}
}
}
</script>
getSelected()function when you call it, and if you expect to have more than one class with the same attribute value(all.length) so there is more than one id with the same attribute value too, ID attribute value must be unique