Created
July 11, 2015 09:00
-
-
Save yusk90/7c3555b6472dcfcf4d54 to your computer and use it in GitHub Desktop.
20.06.15
This file contains 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
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Document</title> | |
<script type="text/javascript" src="script.js"></script> | |
</head> | |
<body> | |
<div id="button-container"> | |
<button id="add-class">Add Class</button> | |
<button id="remove-class">Remove Class</button> | |
<button id="add-text-node">Add Text Node</button> | |
<button id="add-element">Add Element</button> | |
<button id="remove-element">Remove Element</button> | |
<button id="serialize-and-show">Serialize And Show</button> | |
</div> | |
<div id="container">Some text</div> | |
<form id="my-form"> | |
<input type="text" name="first-name"> | |
<input type="text" name="second-name"> | |
</form> | |
</body> | |
</html> |
This file contains 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
window.addEventListener('load', function (e) { | |
var btnContainer = document.getElementById('button-container'), | |
container = document.getElementById('container'), | |
functions = { | |
addElement: function () { | |
var divElem = document.createElement('div'); | |
divElem.textContent = 'some div element'; | |
container.appendChild(divElem); | |
}, | |
addClass: function () { | |
container.classList.toggle('red'); | |
}, | |
serializeAndShow: function () { | |
var myForm = document.getElementById('my-form'), | |
inputInForm = myForm.querySelectorAll('input'), | |
inputArray = Array.prototype.slice.call(inputInForm), | |
fragment = document.createDocumentFragment(), | |
tableElem = document.createElement('table'), | |
tableBody = document.createElement('tbody'), | |
tableRow, | |
tableCell, | |
tableCell2, | |
dataObject = {}, | |
key; | |
inputArray.forEach(function (elem, index) { | |
dataObject[elem.name] = elem.value; | |
}); | |
tableElem.appendChild(tableBody); | |
fragment.appendChild(tableElem); | |
for (key in dataObject) { | |
if (dataObject.hasOwnProperty(key)) { | |
tableRow = document.createElement('tr'); | |
tableCell = document.createElement('td'); | |
tableCell2 = document.createElement('td'); | |
tableCell.textContent = key; | |
tableCell2.textContent = dataObject[key]; | |
tableRow.appendChild(tableCell); | |
tableRow.appendChild(tableCell2); | |
tableBody.appendChild(tableRow); | |
} | |
} | |
container.appendChild(fragment); | |
} | |
}; | |
btnContainer.addEventListener('click', function (event) { | |
var id = event.target.id, // add-element | |
splittedName = id.split('-'), // ['add', 'element'] | |
funcName = splittedName.map(function (elem, index) { | |
if (index === 0) { | |
return elem; | |
} else { | |
return elem[0].toUpperCase() + elem.slice(1); | |
} | |
}), | |
joinedName = funcName.join(''); | |
functions[joinedName](); | |
}, false); | |
}, false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment