Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F2753811
jquery.findWithParent.js
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
1 KB
Referenced Files
None
Subscribers
None
jquery.findWithParent.js
View Options
(
function
()
{
/** @class jQuery */
/**
* Adds support to find parent elements using .closest with less-than selector syntax.
*
* $.findWithParent( $div, "< html div < body" ); // find closest parent of $div "html", find child "div" of it, find closest parent "body" of that, return "body"
* $( '#foo' ).findWithParent( '.bar < .baz' ); // find child ".bar" of "#foo", return closest parent ".baz" from there
*
* @method findWithParent
* @param {jQuery|HTMLElement|string} $context
* @param {string} selector
* @return {jQuery}
*/
function
jQueryFindWithParent
(
$context
,
selector
)
{
$context
=
$
(
$context
);
selector
=
selector
.
trim
();
let
matches
;
while
(
selector
&&
(
matches
=
selector
.
match
(
/(.*?(?:^|[>\s+~]))(<\s*[^>\s+~]+)(.*?)$/
)
)
)
{
if
(
matches
[
1
].
trim
()
)
{
$context
=
$context
.
find
(
matches
[
1
]
);
}
if
(
matches
[
2
].
trim
()
)
{
$context
=
$context
.
closest
(
matches
[
2
].
slice
(
1
)
);
}
selector
=
matches
[
3
].
trim
();
}
if
(
selector
)
{
$context
=
$context
.
find
(
selector
);
}
return
$context
;
}
$
.
findWithParent
=
jQueryFindWithParent
;
/** @class jQuery.fn */
/**
* @param {string} selector
* @return {jQuery}
* @see jQuery#findWithParent
*/
$
.
fn
.
findWithParent
=
function
(
selector
)
{
const
selectors
=
selector
.
split
(
','
);
let
$elements
=
$
();
selectors
.
forEach
(
(
sel
)
=>
{
$elements
=
$elements
.
add
(
jQueryFindWithParent
(
this
,
sel
)
);
}
);
return
$elements
;
};
}()
);
File Metadata
Details
Attached
Mime Type
text/plain
Expires
Fri, Jul 3, 21:45 (16 h, 35 m)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
38/f4/ebcfb2f6fa39f8d0acadbda5aa93
Default Alt Text
jquery.findWithParent.js (1 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment