Vanilla javascript: element.close () not working on element

This is a Sudoko generator, I program in vanilla javascript:

Script with code

More attractive full-screen violin

If you click on one of the fields, the popup will be shown with fields 3x3 from 1 to 9. The problem is that this popup can no longer be closed, although I use the close dialog.

Code, how I generate a sudoku board :

// create sudoku
function tableCreate() {
    var body = document.getElementsByClassName("frame")[0];
    var containerDiv = body.appendChild(document.createElement('div'))
    containerDiv.className = 'container';

    // create single cells with numbers
    function createInnnerCells(parent, xx, yy) {
        for (var x = 1; x <= 3; x++) {
            for (var y = 1; y <= 3; y++) {
                var abc = function () {
                    var div = parent.appendChild(document.createElement('div'))

                    var X = y+yy;
                    var Y = x+xx;
                    var id = 'x' + [X] + 'y' + [Y];

                    var cellValue = sudoku[X][Y]['value'] || '';

                    div.style.background = sudoku[X][Y]['background'] || 'white'
                    div.className = 'cell';
                    div.id = id;

                    var popover = createDialog(id);

                    popover.onclick = function() {
                        popover.close();
                    };

                    div.onclick = function() {
                        popover.show();
                    };

                    div.appendChild(popover);
                    div.appendChild(document.createTextNode(cellValue));
                };
                abc();
            }
        }
    }

    // create big cells for 3x3 single cells
    for (var i = 0; i <= 6; i+=3) {
        for (var j = 0; j <= 6; j+=3) {
            var div = containerDiv.appendChild(document.createElement('div'))
            div.className = 'block';
            createInnnerCells(div, i, j);
        }
    }
}

Note that I apply the close () function to each cell:

popover.onclick = function() {
  popover.close();
};

Code, how I create a popup :

    // create dialog
    function createDialog(position){
        var dialog = document.createElement('dialog');
        dialog.id ='window_'+ position;

        var dialogblock = dialog.appendChild(document.createElement('div'));
        dialogblock.className = 'dialogblock';
        for (var z = 1; z <= 9; z++) {
            var div = dialogblock.appendChild(document.createElement('div'));

            div.className = 'dialogcell';
            div.id = position + 'z'+ z;
            div.appendChild(document.createTextNode(position));
        }

        dialog.onclick = function() {
            dialog.close();
        };

        return dialog;
    }

I also applied the close () dialog

dialog.onclick = function() {
  dialog.close();
};

I do not know why show () works, but close () not?

+4
source share
2 answers

DOM DOM . dialog div. , click dialog, div, , , .

event.stopPropagation.

:

popover.onclick = function(e) {
  e.stopPropagation();
  popover.close();
};

dialog.onclick = function(e) {
  e.stopPropagation();
  dialog.close();
};

: http://jsfiddle.net/p40oahkd/9/

+3

, , close(). element.style.display = "none", . :

dialog.addEventListener('click', function() {
  this.remove();
});

.

+1

Source: https://habr.com/ru/post/1609368/


All Articles