is there an equivalent to jquery's "closest" in Dart -


jquery has "closest" returns closest matching ancestor in tree. there dart equivalent? i'd make following less fragile:

e.target.parent.parent.nextelementsibling.classes.toggle('hide'); 

perhaps like:

e.target.closest('div').nextelementsibling.classes.toggle('hide'); 

there isn't builtin function far know. it's pretty easy code something.

the findclosestancestor() function defined below finds closet ancestor element given ancestor tag:

<!doctype html>  <html>   <head>     <title>ancestor</title>   </head>    <body>        <div id='outer'>       <div id='inner'>         <p></p>       </div>     </div>      <script type="application/dart">       import 'dart:html';        element findclosestancestor(element, ancestortagname) {         element parent = element.parent;         while (parent.tagname.tolowercase() != ancestortagname.tolowercase()) {           parent = parent.parent;           if (parent == null) {             // throw, or find other way handle tagname not being found.             throw '$ancestortagname not found';           }         }         return parent;       }        void main() {         paragraphelement p = query('p');         element parent = findclosestancestor(p, 'div');         print(parent.id); // 'inner'       }         </script>      <script src="packages/browser/dart.js"></script>   </body> </html> 

Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -