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

  • Home
  • SEARCH
  • 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 7063971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T04:46:38+00:00 2026-05-28T04:46:38+00:00

In the following code, when the user clicks on the ‘.more’ span element, I

  • 0

In the following code, when the user clicks on the '.more' span element, I want to retrieve information (eg, the ID) from the img within the article tag using jQuery.

Here’s the HTML:

<article>
<img src="..." id="TR"/>
<div>some text <a>link</a> <span class="more">read more</span></div>
</article>

I’ve tried this code:

   $('.more').click(function () {

        var id = $(this).siblings().siblings('img').attr('id');

});

but it doesn’t seem to work. Any thoughts?

Many thanks!

  • 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-28T04:46:38+00:00Added an answer on May 28, 2026 at 4:46 am

    You can do that like this:

    $(".more").click(function() {
        var id = $(this).closest("article").find("img:first").attr("id");
    });
    

    This looks up the parent tree for the containing <article> tag. Then, it finds the first <img> tag in that and gets its id value.

    You can’t use .siblings() on $(this) because the image isn’t a direct sibling of $(this). You could use .siblings() if you got the right parent that the img was a sibling of, but that’s a little more fragile than what I’ve proposed. For example, this would also work, but it relies on more precise positioning of the HTML objects and any slight edits could break this code:

    $(".more").click(function() {
        var id = $(this).parent().prev().attr("id");
    });
    

    or

    $(".more").click(function() {
        var id = $(this).parent().siblings("img").attr("id");
    });
    

    You can’t use .closest() on the <img> tag directly because the img isn’t a parent of the .more object.

    If this were my code, I’d do it like this because it’s the most flexible and the least likely to break if the HTML is edited slightly and you’re sure to get the right img object, even if there are more images nearby:

    <article>
    <img class="target" src="..." id="TR"/>
    <div>some text <a>link</a> <span class="more">read more</span></div>
    </article>
    
    $(".more").click(function() {
        var id = $(this).closest("article").find(".target").attr("id");
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code will search for the user within the domain controller, but I
I have the following code that loads a user's Active Directory DirectoryEntry object from
In the following code, the user selects a customer from the ComboBox and that
If we have the following code, then when user clicks an Edit button, page
Hi I have the following code in a form . When the user clicks
I used the following code to retrieve an image from a url and display
I have the following code running when the user clicks the Save button: -
I have following code class User attr_accessor :name end u = User.new u.name =
The following code throws System.UriFormatException: var uri = new UriBuilder(ftp://user:pass#word@ftp.somewhere.com:21/fu/bar.zip); System.UriFormatException: Invalid URI: A
The following code is implemented in Page_Load event to show SaveFileDialog to the user

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.