Javascript DOM Create Element


/ Published in: JavaScript
Save to your folder(s)

Javascript DOM Create Element


Copy this code and paste it in your HTML
  1. $("<a/>", {
  2. id: 'example-link',
  3. href: 'http://www.example.com/',
  4. text: 'Example Page'
  5. }).appendTo("body");
  6.  
  7. var myRow = document.createElement("p");
  8. myRow.class = "myClass";
  9. myRow.id = 'mycustomdiv'
  10. myRow.innerHTML = elements;
  11. var textcontent = document.createTextNode('Hello All')
  12. myRow.appendChild(textcontent)
  13.  
  14. document.getElementsByTagName('body')[0].appendChild(myRow);
  15. document.getElementById("myContainer").appendChild(myRow);
  16. _________
  17. var elem = document.getElementById('container');
  18. var child = elem.children[0];
  19. var child = elem.firstElementChild;//== children[0]
  20.  
  21. var child = elem.childNodes[0];//or childNodes[1]
  22. var child = elem.childNodes;//treat as Nodelist
  23. var child = elem.firstChild;
  24.  
  25. console.log(elem.childNodes[1]);
  26. console.log(elem.children[0]);
  27. console.log(elem.firstElementChild);
  28.  
  29. // CloneNode
  30.  
  31. $('li').bind('click', function(){
  32.  
  33. var select = $(this).find('section')
  34. var dupeSelect = select[0].cloneNode(true);
  35. $('#uphole').html(dupeSelect);
  36.  
  37. });

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.