Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6766583
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:50:55+00:00 2026-05-26T14:50:55+00:00

jQuery has a lovely if somewhat misnamed method called closest() that walks up the

  • 0

jQuery has a lovely if somewhat misnamed method called closest() that walks up the DOM tree looking for a matching element. For example, if I’ve got this HTML:

<table src="foo">
  <tr>
    <td>Yay</td>
  </tr>
</table>

Assuming element is set to <td>, then I can figure the value of src like this:

element.closest('table')['src']

And that will cleanly return “undefined” if either of the table element or its src attribute are missing.

Having gotten used to this in Javascriptland, I’d love to find something equivalent for Nokogiri in Rubyland, but the closest I’ve been able to come up with is this distinctly inelegant hack using ancestors():

ancestors = element.ancestors('table')
src = ancestors.any? ? first['src'] : nil

The ternary is needed because first returns nil if called on an empty array. Better ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:50:56+00:00Added an answer on May 26, 2026 at 2:50 pm

    You can call first on an empty array, the problem is that it will return nil and you can’t say nil['src'] without getting sad. You could do this:

    src = (element.ancestors('table').first || { })['src']
    

    And if you’re in Rails, you could use try thusly:

    src = element.ancestors('table').first.try(:fetch, 'src')
    

    If you’re doing this sort of thing a lot then hide the ugliness in a method:

    def closest_attr_from(e, selector, attr)
      a = e.closest(selector)
      a ? a[attr] : nil
    end
    

    and then

    src = closest_attr_from(element, 'table', 'src')
    

    You could also patch it right into Nokogiri::XML::Node (but I wouldn’t recommend it):

    class Nokogiri::XML::Node
      def closest(selector)
        ancestors(selector).first
      end
      def closest_attr(selector, attr)
        a = closest(selector)
        a ? a[attr] : nil
      end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

jQuery has a new method called on() that is recommended to replace delegate() ,
jQuery has a method that is automatically called prior to the success callback of
jQuery has functions like .on() and .trigger() that let you attach events to DOM
Jquery has a great language construct that looks like this: $(document).ready(function() { $(a).click(function() {
The JQuery has method effectively selects all elements where they have particular descendants. I
I have a jquery menu that has so many entries that it extends further
I'm using a jQuery plugin that has its functions defined as such: $('#mydiv').pluginAction({ someproperty:
I created a page that has a JQuery based dialog using the standard JQuery
Does anyone know of a jQuery plugin that has 'helpers' or extensions like those
jQuery has the very cool feature/method .data, I wonder if there is a way

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.