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 9140311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:29:59+00:00 2026-06-17T09:29:59+00:00

Possible Duplicate: Getting a parent element for a jquery selector I have a list

  • 0

Possible Duplicate:
Getting a parent element for a jquery selector

I have a list of item :

        <div id="wrap"><ul class="thumbs noscript">
        <li id="1">
        <a class="thumb" href="photos/10-01.jpg" title="" />
        <img src="photos/10-01_thumb.jpg" alt="" />
        </a>
        <div class="caption">
        <div class="download"><a href="#" class="add2cart">Save Image</a></div>
        <div class="image-title">Titre de la photo #1</div>
        <div class="image-desc">Description de la photo #1</div>
        </div>
        </li> /* and so on with li's and different ID's... */

I can’t get the value of the li ID, therefore, the title.
Have read and tried almost every answer on here, they won’t work once I test on my site…
Any idea ? TY folks

$(document).ready(function(){
    $("#wrap > ul > li > .caption > .download > .add2cart").click(function(){
        var thisID = $(this).closest('li').attr("id");
        alert(thisID);
    });
});

RIGHT ANSWER (see below) : insomiac, tyvm
And thanks also to Diodeus for pointing out ID problem.
Thanks to all the folks who answered 😉

  • 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-06-17T09:30:00+00:00Added an answer on June 17, 2026 at 9:30 am

    Store the value using “data” api and then on click retrieve that value instead of using id.

    <li id="img" data-val="1">
                <a class="thumb" href="photos/10-01.jpg" title="" />
                <img src="photos/10-01_thumb.jpg" alt="" />
                </a>
                <div class="caption">
                <div class="download"><a href="#" class="add2cart">Save Image</a></div>
                <div class="image-title">Titre de la photo #1</div>
                <div class="image-desc">Description de la photo #1</div>
                </div>
        </li> /* and so on with li's and different ID's... */
    

    jquery :

        $(document).ready(function(){
            // if you want the value of li, using the child click.
            // Do something like this, if you want the click on "thumb".
            $('.thumb').click(function(e){
                 e.preventDefault();
                var val = $(this).closest('li').data('val');
             });
    
         });
    

    Works on jsfiddle

    Update to your js code:

    $(document).ready(function(){
        $(".add2cart").click(function(e){
            e.preventDefault();
            // Use prop if your jquery version is above 1.6.. 
            var thisID = $(this).closest('li').prop("id");
            alert(thisID);
        });
    });
    

    Note : As of jQuery 1.6, the .attr() method returns undefined for attributes that have not been set. In addition, .attr() should not be used on plain objects, arrays, the window, or the document. To retrieve and change DOM properties, use the .prop() method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Getting the ID of the element that fired an event using JQuery
Possible Duplicate: Getting the ID of the element that fired an event using jQuery
Possible Duplicate: Getting jQuery-inserted DOM elements without requerying for element after insertion Is there
Possible Duplicate: Getting list of parameters inside python function E.g. supposes I have def
Possible Duplicate: Learning jQuery and Getting Better at Javascript I have found jQuery very
Possible Duplicate: Getting the ID of the element that fired an event using JQuery
Possible Duplicate: Getting JavaScript object key list How to iterate json data in jquery
Possible Duplicate: # in url getting ignored in php 5.2 I have following link
Possible Duplicate: Getting an issue while checking the dynamically generated checkbox through list view
Possible Duplicate: Getting domain name without TLD So I have a Web site I

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.