Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add `fn.parent()` method

  • Loading branch information...
commit 784de340e1e5eabf8959af1259ada9ebc1e46ee4 1 parent 4eca863
@mislav mislav authored
Showing with 19 additions and 0 deletions.
  1. +1 −0  README.rdoc
  2. +8 −0 src/zepto.js
  3. +10 −0 test/zepto.html
View
1  README.rdoc
@@ -48,6 +48,7 @@ Context and .find calls are equivalent:
find('selector'): find all children/grandchildren that match the given selector
closest('selector'): find the first matching element by going upwards starting from the current element
+ parent(): immediate parent node of each element in collection
next(): next siblings
prev(): previous siblings
is('selector'): returns true/false if first element matches the selector
View
8 src/zepto.js
@@ -76,6 +76,14 @@ var Zepto = (function() {
while(node && node !== document && nodes.indexOf(node) < 0) node = node.parentNode;
return $(node);
},
+ parent: function(selector){
+ var node, nodes = [];
+ this.each(function(el){
+ if ((node = el.parentNode) && nodes.indexOf(node) < 0) nodes.push(node);
+ });
+ nodes = $(nodes);
+ return selector === undefined ? nodes : nodes.filter(selector);
+ },
pluck: function(property){ return this.dom.map(function(element){ return element[property] }) },
show: function(){ return this.css('display', 'block') },
hide: function(){ return this.css('display', 'none') },
View
10 test/zepto.html
@@ -58,6 +58,9 @@
<li>two</li>
<li>three</li>
</ul>
+ <ul>
+ <li></li>
+ </ul>
</li>
</ul>
@@ -342,6 +345,13 @@
t.assertLength(0, el.closest('div'));
},
+ testParent: function(t){
+ var el = $('#li1');
+ t.assertEqualCollection($('#parents'), el.parent());
+ t.assertEqualCollection($('#li1 > ul'), el.find('li').parent());
+ t.assertLength(0, $(document.createElement('div')).parent());
+ },
+
testFind: function(t){
var found = $('p#find1').find('span.findme');
t.assertLength(4, found);
Please sign in to comment.
Something went wrong with that request. Please try again.