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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:44:12+00:00 2026-06-10T21:44:12+00:00

I am trying to get the ID attribute of anchors which has class current,

  • 0

I am trying to get the ID attribute of anchors which has class current, or their parent LI has class current.

I try this code:

    var terms = '';
    $('.product-tax li').each(function(){
        if ($(this).hasClass('current')) {
            terms += $(this).children('a').attr('id') + ':';
        } else if ($(this).children('a').hasClass('current')) {
            terms += $(this).children('a').attr('id') + ':';
        }
    });

But it does not work, it gives unexpected result. I want to grab the ID inside anchor, for the classes “current” for either LI or anchor.

Basically I am using each because there is more than one .product-tax and the user makes a selection but the class “current” can be added to LI, or anchor that is why I need to check. This code does not work.

Any suggestions?

HTML:

<div class="product-tax product-color">
    <h3>Choose color</h3>
    <ul>
        <li><a href="#" id="color-pink" style="background: Pink;border: 1px solid Pink;"></a></li>
    </ul>
</div>


<div class="product-tax product-size">
    <h3>Choose size</h3>
    <ul>
        <li><a href="#" id="size-2s">2(s)</a></li>
        <li><a href="#" id="size-3m">3(m)</a></li>
        <li><a href="#" id="size-4l">4(l)</a></li>
        <li><a href="#" id="size-5xl">5(xl)</a></li>
        <li><a href="#" id="size-6xxl">6(xxl)</a></li>
    </ul>
</div>
  • 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-10T21:44:14+00:00Added an answer on June 10, 2026 at 9:44 pm

    You can select all anchor children of the LI elements, and all children with the current class like this:

    var terms = "";
    $(".product-tax li.current > a, .product-tax li > a.current").each(function(){
        terms = terms + $(this).attr("id") + ":"
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to get the data attribute in this way (my doctype is html5):
I am trying to get the attribute code from the filter.phtml template file. Does
I'm trying to get the href attribute from a div with the class name
I am trying to get a attribute of a date picker to pull the
I'm trying to get the source attribute of all images withing a specific div
We're trying to get a conditional attribute to work, case in point, there's a
I'm trying to access attribute with interpolation (Saas 3), but get an error. @each
I am trying to get all of the video elements and their attributes from
So i am trying get 2 div-containers which both should contain centered text (Both
I'm trying get all of the current stack frames and do some inspection on

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.