Guest User

Untitled

a guest
Jul 28th, 2016
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. createElement: (type, props, children) => {
  2. let el = document.createElement(type),
  3. key;
  4.  
  5. for (key in props) {
  6. if (props.hasOwnProperty(key)) {
  7. if (helpers.shouldAssignVal(key)) {
  8. el[key] = props[key];
  9. } else {
  10. let attr = helpers.camelCaseToDash(key),
  11. i = 0;
  12.  
  13. if (helpers.isArray(props[key])) {
  14. for (i; i < props[key].length; i++) {
  15. el.setAttribute(attr, props[key][i]);
  16. }
  17. } else {
  18. el.setAttribute(attr, props[key]);
  19. }
  20. }
  21. }
  22. }
  23.  
  24. if (typeof children !== 'undefined') {
  25. if (helpers.isArray(children)) {
  26. for (let y = 0; y < children.length; y++) {
  27. if (children[y].nodeType) {
  28. el.appendChild(children[y]);
  29. }
  30. }
  31. } else {
  32. el.appendChild(children);
  33. }
  34. }
  35.  
  36. return el;
  37. }
  38.  
  39. let h1 = helpers.createElement('h1', {
  40. class: 'title',
  41. id:'title,
  42. innerText: 'This is a title' // This part will NOT set an attribute, but will set the value of the innerText property of this node.
  43. });
  44.  
  45. <h1 class="title" id="title">This is a title</h1>
  46.  
  47. shouldAssignVal: key => {
  48. return key === 'value' || key === 'innerText' || key === 'innerHtml';
  49. }
Add Comment
Please, Sign In to add comment