Closed figuerom16 closed 8 months ago
Interesting. Might add next / previous to core.
As a chainable ex: me().next()
and me().previous()
it'd be no problem
EDIT: refactor for clarity.
That unfortunately looks like it's going to grab the parent element then next/prev sibling which would dodge grabbing a void element.
What about having me() check if there is a previousElementSibling first if that doesn't exist then use the parent instead?
me(selector=null, start=document, warning=true) {
if (!selector) return $.sugar(start.currentScript.previousElementSibling || start.currentScript.parentElement) // Just local me() in <script>
...
},
any(selector, start=document, warning=true) {
if (!selector) return $.sugar([start.currentScript.previousElementSibling || start.currentScript.parentElement]) // Just local me() in <script>
...
},
That will allow for the capture void elements like input below.
<div class="flex">
<input type="search" placeholder="Search">
<script>me().on('input', e=>{search_table(me('#tmarkdown'), me(e))})</script>
...
</div>
me() would retrieve input instead of the current behavior of retrieving the div. Moving script directly below <div class="flex">
would cause it to get the parent element instead since no previous sibling exists.
I was experimenting with some tree traversal over here. https://github.com/gnat/surreal/pull/6 With that system your case would be something like me.el('input').on(...)
to grab the first input
element of me
(which would still be the div)
@gazpachoking That looks interesting and is beyond what I'm looking for. My objective is to be as lazy as possible and retrieve previous adjacent element or the parent. Advanced traversal is beyond me other than using CSS selectors.
I refactored my previous comment for clarity.
Closing this in favor of PR #17
This adds in a small modifier to pick up elementSiblings. It's been great for picking up single tags at the same level like
<input>
and<img>
. Putting some of the scripting outside of a tag like<td>
is nice for for searching a table without creating a text node.Is it possible to use the sugar system for this?