Guest User

Untitled

a guest
Jul 17th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.89 KB | None | 0 0
  1. <div data-type='project' id='example1'>
  2. <input name='start-date'/>
  3. <div data-type='project-lead' id='example2'>
  4. <input name='department'/>
  5. <input name='email'/>
  6. <div data-type='analyst'>
  7. <input name='department'/>
  8. <input name='email'/>
  9. </div>
  10. <div data-type='analyst'>
  11. <input name='department'/>
  12. <input name='email'/>
  13. </div>
  14. <div data-type='analyst'>
  15. <input name='department'/>
  16. <input name='email'/>
  17. </div>
  18. </div>
  19. <div class="JustToMakeMyLifeMoreDifficult">
  20. <div data-type='sponsor'>
  21. <input name='department'/>
  22. <input name='email'/>
  23. </div>
  24. <div data-type='sponsor'>
  25. <input name='department'/>
  26. <input name='email'/>
  27. </div>
  28. </div>
  29. </div>
  30.  
  31. myData($obj){
  32. return $obj.find('[data-type]').not([data-type elements further down]);
  33. }
  34.  
  35. myData($('#example1'))
  36. myData($('#example2'))
  37.  
  38. [project-lead,sponsor,sponsor]
  39. [analyst, analyst, analyst]
  40.  
  41. (function( $ ){
  42. $.fn.dataChildren = function(_selector) {
  43. var iter = this;
  44. var res = this.children(_selector);
  45. while ( ( iter = iter.children(':not(' + _selector +')') ).length ) {
  46. res = res.add(iter.children(_selector));
  47. }
  48. return res;
  49. };
  50. })( jQuery );
  51.  
  52. $('#example1').dataChildren('[data-type]')
  53.  
  54. var el = $('#example1');
  55.  
  56. var res = el.children('[data-type]');
  57.  
  58. while ( ( el = el.children(':not([data-type])') ).length ) {
  59. res = res.add(el.children('[data-type]'));
  60. }
  61.  
  62. var el = $('#example1'); // el is the current level
  63. var res = $(); // res holds the result
  64. var data_types; // holds the children with data-type for the current level
  65.  
  66. do {
  67. // from current level, get children with data-type
  68. data_types = el.children('[data-type]');
  69.  
  70. // add those to the result set
  71. res = res.add( data_types );
  72.  
  73. // make the current level be the children of the current level that
  74. // do NOT have data-type
  75. el = el.children().not( data_types );
  76.  
  77. } while( el.length ); // continue as long as the new current level
  78. // has at least one element
  79.  
  80. var ex = $('#example1');
  81.  
  82. var res = ex.find('> [data-type], > * > [data-type]');
  83.  
  84. var ex = $('#example1');
  85.  
  86. var res = ex.find('> [data-type], > :not([data-type]) > [data-type]');
  87.  
  88. '> [data-type]'
  89.  
  90. '> :not([data-type]) > [data-type]'
  91.  
  92. function getNodes(id) {
  93. var el = (typeof id == 'string')? document.getElementById(id) : id;
  94. var result = [];
  95. var node, nodes = el.childNodes;
  96. var prop = 'data-type';
  97. var tag = 'div';
  98.  
  99. for (var i=0, iLen=nodes.length; i<iLen; i++) {
  100. node = nodes[i];
  101.  
  102. if (node.tagName && node.tagName.toLowerCase() == tag) {
  103.  
  104. if (node.getAttribute(prop)) {
  105. result.push(node);
  106.  
  107. } else {
  108. result = result.concat(getNodes(node));
  109. }
  110. }
  111. }
  112. return result;
  113. }
  114.  
  115. $obj.find('> [data-type]');
Add Comment
Please, Sign In to add comment