<div id="app">
<button class="add-item">Добавить блок</button>
<div class="menu__list"></div>
</div>
.menu__list {
display: flex;
flex-wrap: wrap;
}
.added-block {
display: flex;
width: 120px;
padding: 1em;
margin: 1em;
border: 1px solid #CCC;
}
.menu__item {
margin: 4px 5px;
}
const btn = document.querySelector('button.add-item');
const createBlockHTML = text => `
<div class="added-block">
<div class="menu__item ">${text}</div>
<button>×</button>
</div>
`;
const container = document.querySelector('div.menu__list');
const addBlock = () => {
container.innerHTML += createBlockHTML('Lorem ' + Math.random().toString(16).substring(2,6));
};
btn.addEventListener('click', addBlock);
container.addEventListener('click', ({ target }) => {
if (target.nodeName === 'BUTTON') {
target.parentElement.remove();
}
});
addBlock();
addBlock();
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.