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
Post a Comment