Skip to content

Instantly share code, notes, and snippets.

@dmjcomdem
Created July 24, 2017 20:46
Show Gist options
  • Save dmjcomdem/6d071eb1437b7ff3fad0544f26e7277b to your computer and use it in GitHub Desktop.
Save dmjcomdem/6d071eb1437b7ff3fad0544f26e7277b to your computer and use it in GitHub Desktop.
Создание DOM-элемента, с указанными атрибутами
function createElement(tag, props, ...children) {
const element = document.createElement(tag);
Object.keys(props).forEach(key => {
if (key.startsWith('data-')) {
element.setAttribute(key, props[key]);
} else {
element[key] = props[key];
}
});
children.forEach(child => {
if (typeof child === 'string') {
child = document.createTextNode(child);
}
element.appendChild(child);
});
return element;
}
// createListItem(todo) {
// const checkbox = createElement('input', { type: 'checkbox', className: 'checkbox', checked: todo.completed ? 'checked' : '' });
// const label = createElement('label', { className: 'title' }, todo.title);
// const editInput = createElement('input', { type: 'text', className: 'textfield' });
// const editButton = createElement('button', { className: 'edit' }, 'Изменить');
// const removeButton = createElement('button', { className: 'remove' }, 'Удалить');
// const item = createElement('li', { className: `todo-item${todo.completed ? ' completed': ''}`, 'data-id': todo.id }, checkbox, label, editInput, editButton, removeButton);
// }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment