get a selected contenteditable class with dynamically generated contenteditables

I was able to solve it myself by checking, using a for loop, if a cell is selected, and then deleting the content of that cell.

Here’s the code

// Update Cells
function check() {
    for (let i = 1; i <= num ** 2; i++) {
        let cla = ".cell" + i.toString();
        if ($(cla).is(":focus")) {
            bo[i - 1] = "";
        }
    }
}
function save() {
    for (let i = 0; i < num ** 2; i++) {
        let c = document.getElementsByClassName("cell")[i];
        if (c.innerHTML == "") {
            bo[i] = "0";
        } else {
            bo[i] = c.innerHTML;
        }
    }
}

// Refreshing
document.addEventListener('keydown', function(e) {
    switch (e.key) {
        case "1":
        case "2":
        case "3":
        case "4":
        case "5":
        case "6":
        case "7":
        case "8":
        case "9":
            save();
            break;
        case "Backspace":
            save();
            check();
            draw();
            break;
        case "ArrowLeft":
        case "ArrowRight":
            break;
        default:
            save();
            draw();
    }
})

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top