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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T08:06:35+00:00 2026-05-24T08:06:35+00:00

I’m trying to find an element with the class active and then echo what

  • 0

I’m trying to find an element with the class “active” and then echo what other classes it has, and only select the start of the string.

HTML:

<div class="cmButtons">
    <ul>
        <li class="cmAuto_btn a active">Auto BTN</li>
        <li class="cmPro_btn a">Pro BTN</li>
        <li class="cmFocus_btn a">Focus BTN</li>
        <li class="cmBright_btn a">Bright BTN</li>
        <li class="cmLow_btn a">Low BTN</li>
        <li class="cmMotion_btn a">Motion BTN</li>
        <li class="cmCustom_btn a">Custom BTN</li>
    </ul>
</div>

CSS:

#result{background:#EEE;border:1px solid #CCC;margin:20px 0;height:30px;line-height:30px;width:500px;}

Javascript (with jQuery):

if($('.cmButtons ul li').hasClass("active")){
    var activeLi = $(this).attr("class");
    $('#result').html(activeLi);
}

Demo on JS Fiddle.

So, based on the fiddle;

The active element is: <li class="cmAuto_btn a active">Auto BTN</li>

jQuery should output the class as: cmAuto_btn a active

jQuery should then modify the string to render as cmAuto_btn

How can I do this?

  • 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-24T08:06:36+00:00Added an answer on May 24, 2026 at 8:06 am

    You need to iterate over the set of matched elements with each. You can then split the class names on spaces, and get the first element of the resulting array:

    $('.cmButtons ul li').each(function() {
        if($(this).hasClass("active")) {
           var firstClass = $(this).attr("class").split(" ")[0];
        } 
    });
    

    Here’s an updated example.

    And here’s an alternative, due to comments below (still using each in case there are multiple elements with class active):

    $('.cmButtons ul li.active').each(function() {
        var firstClass = $(this).attr("class").split(" ")[0];
    });
    

    You should probably use this one, unless you also need to do something with elements that do not have the active class, in which case you can use the above example with an else block.

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

Sidebar

Related Questions

I'm trying to find the next element with a class of error and hitting
I'm trying to find the class .post-info inside the parent of a clicked element,
I'm trying to find text inside an element whose class is either myClass1 OR
I am trying to find an example of placing an element above the Table
The following function is trying to find the nth to last element of a
For a Jericho Element, I am trying to find out how to loop over
I'm trying to use XPath to find all elements that have an element in
I'm trying to write javascript to find page elements relative to a given element
I want to find an element by class name. I know it will appear
I'm having a problem with trying to find out whether the parent element does

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.