Created
July 24, 2017 20:46
-
-
Save dmjcomdem/6d071eb1437b7ff3fad0544f26e7277b to your computer and use it in GitHub Desktop.
Создание DOM-элемента, с указанными атрибутами
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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