javascript - Traversing up and finding a child element with attribute equal to a specific value -
i have list ul
contains element article has attribute data-level
. traversing in javascript through articles. adding replyto symbol comments have data-level
larger 1, username of parent, need traverse comment list , find article element has data-level="0"
, username element.
i can't use jquery closest()
because doesn't check inside element. if use parent().parent()
run closest() on parent, not good.
how can run closest()
function or familiar on article element (red arrow in image) find first article element above has attribute equals 0 (data-level="0"
).
here's html code:
<ul> <li> <article itemprop="comment" itemscope="itemscope" itemtype="http://schema.org/comment" data-user-id="28" id="comment-40" data-comment-id="40" class="comment-wrapper" data-level="0"> <header> <p class="comment-title"> <span class="submitted-by"><span itemprop="author" itemscope="itemscope" itemtype="http://schema.org/person"><span itemprop="name">yariv</span></span><span class="karma" title="karma"><span class="fa fa-star"></span>0</span> • <meta itemprop="datecreated" content="2015-12-09t06:28:59z"> <time class="date-submitted" datetime="2015-12-09t06:28:59z">7 months ago</time> </span> </p> </header> <div class="comment-content"> <p class="comment-body" itemprop="text">fgdfg </p> </div> <div class="comment-options"><span data-already-voted="false" class="upvote"><span class="fa fa-thumbs-up"></span><span class="upvote-num" itemprop="upvotecount">1</span></span><span class="reply" data-user-id="28" data-comment-id="40">reply</span><span class="flag-comment">flag</span> </div> <div class="notification"></div> </article> </li> <li> <article itemprop="comment" itemscope="itemscope" itemtype="http://schema.org/comment" data-user-id="28" id="comment-42" data-comment-id="40" class="comment-wrapper" data-level="2"> <header> <p class="comment-title"> <span class="submitted-by"><span itemprop="author" itemscope="itemscope" itemtype="http://schema.org/person"><span itemprop="name">yariv</span></span><span class="karma" title="karma"><span class="fa fa-star"></span>0</span> • <meta itemprop="datecreated" content="2015-12-09t06:28:59z"> <time class="date-submitted" datetime="2015-12-09t06:28:59z">7 months ago</time> </span> </p> </header> <div class="comment-content"> <p class="comment-body" itemprop="text">fgdfg </p> </div> <div class="comment-options"><span data-already-voted="false" class="upvote"><span class="fa fa-thumbs-up"></span><span class="upvote-num" itemprop="upvotecount">1</span></span><span class="reply" data-user-id="28" data-comment-id="40">reply</span><span class="flag-comment">flag</span> </div> <div class="notification"></div> </article> </li> </ul>
note: there can several comments data-level="0"
, need closest 1 above article checking. in other words. <article>
element, need find closest (from above) <article>
element exists above inside li
element it's attribute data-level
equals zero.
you can use closest
li
around reply, , prevall
:has([data-level=0])
filter, , first
nearest (prevall
returns them in order of nearness rather usual document order):
var main = startingelement .closest("li") .prevall(":has([data-level=0])") .first() .find("[data-level]");
simplified example:
// our starting point: var startingelement = $("#start"); // find "nearest" data-level=0 var main = startingelement .closest("li") .prevall(":has([data-level=0])") .first() .find("[data-level]"); // show got console.log(main.text());
<ul> <li> <div data-level="0">first level 0</div> </li> <li> <div data-level="1">first level 1</div> </li> <li> <div data-level="2">first level 2</div> </li> <li> <div data-level="0">second level 0</div> </li> <li> <div data-level="1">second level 1</div> </li> <li> <div data-level="2" id="start">second level 2</div> </li> <li> <div data-level="0">third level 0</div> </li> <li> <div data-level="1">third level 1</div> </li> <li> <div data-level="2">third level 2</div> </li> </ul> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
note how our starting point li
2 siblings away 1 containing data-level="0"
, skipped 1 in-between because didn't match :has
condition.
Comments
Post a Comment