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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:04:49+00:00 2026-06-03T05:04:49+00:00

I found a nice snippet here on Stack which grabs the text from a

  • 0

I found a nice snippet here on Stack which grabs the text from a link and appends it to the a tag as a class:

$('a.nav').each(function() {

    // add class with name of the link's text
    $(this).addClass($(this).text());
});

It works great except that I have a list of linked search results that output (numbers), e.g. (19) after the link text.

So the structure of my HTML after the JQuery above is applied is as such:

<li><a class="Basic page (1)" href="#">Basic page (1)</a></li>
<li><a class="Blog (19)" href="#">Blog (19)</a></li>
<li><a class="News (4)" href="#">News (4)</a></li>

As you can see, it’s not ideal. If possible, I’d like to at least get rid of the numbers in parenths i.e. (1), (19) etc… and then put dashes and with the text and lower case. Note this is for a faceted search result so the links will never all be together or in the same order. I am simply trying to apply dynamic classes based on the link text name so I can do other things down the road in a theming realm.

so this:

<li><a class="Basic page (1)" href="#">Basic page (1)</a></li>

… would become this:

<li><a class="basic-page" href="#">Basic page (1)</a></li>
  • 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-03T05:04:50+00:00Added an answer on June 3, 2026 at 5:04 am

    Try something like below,

    DEMO

    $(function() {
        $.each($('ul li a'), function() {
            var text = $(this).text();
            this.className = $.trim(text.replace(/\(\d*\)/g, '').toLowerCase()).replace(/\s/, '_');
        });
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Ok so here is the problem, I found a nice working 2D Camera from:
A while back I found a really nice snippet which showed how a file
I found this nice snippet of code online: rkApp = Registry.LocalMachine.OpenSubKey(SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run, true); Which runs
I've found very nice solution of retrieving images from db/blob thanks to How to
I found a nice Find Name code snippet that I'm using in a WPF
I found a nice way to use the audio tag, Audio for everybody .
I'm putting together a nav bar and found a nice script to allow a
A few days ago I found a nice snippet on SO to test memory
I need to draw a pie chart. I found a nice tutorial here http://mac-objective-c.blogspot.com/2009/04/drawing-pie-charts.html
I found a nice script to import xml using powershell http://slash4.de/tutorials/Automatic_mediawiki_page_import_powershell_script Currently I don't

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.