36226

Selecting the last child

Question:

I was wondering how I'd get an alert of a last child by clicking a link.

<h3 id="comment:5" class="myclass">

This is how far I got:

$('#lastcomment').click(function() { alert('Handler for .click() called.'); });

My aim is to jump to the last comment by clicking the link. An alert would be fine for the first step I guess. So that I can see the last child of the comments.

Any help is much appreciated.

Answer1:

Use the <a href="http://api.jquery.com/last-selector/" rel="nofollow">:last</a> selector or <a href="http://api.jquery.com/last/" rel="nofollow">.last()</a> method:

$("#lastcomment").click(function () { var lastComment = $("h3[id^=comment]:last"); alert(lastComment.attr("id")); }

Answer2:

you can use jQuery's :last selector. but i need to see more HTML to make something work...

Answer3:

So you have a link that you want to go to the last comment on a page? Why not give the comment an id of comment-12 (for example), then just change your anchor to relfect this:

<a class="myclass" href="#comment-12" id="last-comment"> </a>

If you are wanting to do this in javascript, something like:

$('#lastcomment').click(function() { var numComments = $(".comments").length; // Assuming your comments have a class window.location = window.location.href + "#" + $(".comments").eq(numComments).attr("id"); });

Recommend

  • The specified type member is not supported in LINQ to Entities
  • How are these Java byte offsets calculated?
  • Android history stack clear without one
  • Quadratically constrained MIQP with julia and gurobi
  • Calculate quantiles for large data
  • Typedef with two sets of brackets?
  • How to animate float:left divs? [closed]
  • UINavigationController alternatives
  • how to find function boundaries in binary code
  • moving an object without actually touching the object
  • FindAndModify, return array of Objects
  • Python importing a function with file's name contained in a variable
  • Constant embedded for loop condition optimization in C++ with gcc
  • Matching multiple strings to one long string using patterns
  • Implementing “Add to Home Screen”, ala Safari
  • Why was the Profile provider not built into Web Apps?
  • Android onKey w/ virtual keyboard
  • netsh acl setting (need alternative method - registry settings?)
  • how to set variables in a php include file?
  • Diff between two dataframes in pandas
  • Detect when Facebook like button is clicked
  • AJAX Html Editor Extender upload image appearing blank
  • Use of this Javascript
  • MailKit: The IMAP server replied to the 'EXAMINE' command with a 'BAD' response
  • Recording logins for password protected directories
  • How to add a column to a Pandas dataframe made of arrays of the n-preceding values of another column
  • jquery mobile loadPage not working
  • Why winpcap requires both .lib and .dll to run?
  • Data Validation Drop Down Box Arrow Disappearing
  • How to set the response of a form post action to a iframe source?
  • How do you join a server to an Active Directory (domain)?
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • How does Linux kernel interrupt the application?
  • Are Kotlin's Float, Int etc optimised to built-in types in the JVM? [duplicate]
  • unknown Exception android
  • Binding checkboxes to object values in AngularJs
  • git trying to push non-existent file … after clearing cache
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?