Skip to content

Instantly share code, notes, and snippets.

@waako
Created February 21, 2012 08:43
Show Gist options
  • Save waako/1875199 to your computer and use it in GitHub Desktop.
Save waako/1875199 to your computer and use it in GitHub Desktop.
Get value of last nested li and output in element of same parent
<!-- Trying to print title value of :last li.nested.one in the closest div.header -->
<ul>
<li class="parent">
<div class="header"></div>
<ul>
<li class="nested one">
<li class="nested one">
<li class="nested one" title="first">
<li class="nested two">
<li class="nested two">
</ul>
</li>
<li class="parent">
<div class="header"></div>
<ul>
<li class="nested one">
<li class="nested one">
<li class="nested one" title="second">
<li class="nested two">
<li class="nested two">
</ul>
</li>
<li class="parent">
<div class="header"></div>
<ul>
<li class="nested one">
<li class="nested one">
<li class="nested one" title="third">
<li class="nested two">
<li class="nested two">
</ul>
</li>
<li class="parent">
<div class="header"></div>
<ul>
<li class="nested one">
<li class="nested one">
<li class="nested one" title="fourth">
<li class="nested two">
<li class="nested two">
</ul>
</li>
</ul>
<script>
var title = $('li.nested.one:last', 'li.parent').attr('title');
$('li.nested.one:last', 'li.parent').each(function() {
$(this).closest('.header').text(title);
});
</script>
@jacobpayne89
Copy link

Try this mate

$('li.parent').each(function(){
    var lastOne = $('ul li.one:last', this).text();
    $('.header', this).text(lastOne);
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment