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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:20:05+00:00 2026-06-04T08:20:05+00:00

I’m trying to do something similar to this question , but it’s a bit

  • 0

I’m trying to do something similar to this question, but it’s a bit different, so the solution there isn’t working for me.

<span class="a-class another-class test-top-left"></span>

I have an element (this code shows a span but it could be div span or anything). This element has a class beginning with test- (test-top-left, test-top-right etc.) I’ve triggered a click event on classes starting with test- and saved the clicked object as var object = this;. Simple stuff so far.

What I’m trying to do now is get the full name of that class (test-top-left). I know it starts with test- but what’s the full name. The thing is that there are other classes a-class another-class and test-top-left. Can hasClass be used to get the full name of the class? I’d prefer not to use find() or filter() just because there may be additional elements within that also have class="test-"

Edit:

The code I have now is, but it gives me ALL the classes. What I need is the single class beginning with test-.

var object = this;
$(object).attr('class'); 

So now I for loop through all the classes and test each one separately, which seems like a lot of unnecessary code. I’m hoping jQuery has a clever way to get the exact class that was clicked right away.

  • 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-04T08:20:06+00:00Added an answer on June 4, 2026 at 8:20 am

    Description

    You can use jQuerys Attribute Contains Selector, .attr() and .click() method.

    Attribute Contains Selector – Selects elements that have the specified attribute with a value containing the a given substring.

    .attr() – Get the value of an attribute for the first element in the set of matched elements.

    .click() – Bind an event handler to the "click" JavaScript event, or trigger that event on an element.

    Sample

    html

    <span class="anyclass test-hello">Hello World</span>​
    

    jQuery

    $("[class*='test']").click(function() {
        var object = $(this);
        alert(object.attr("class").match(/(test-.*?)(?:\s+|$)/)[1])
    ;});
    

    Check out the updated jsFiddle

    Update

    If you dont want to use regex you can do this.

    $("[class*='test']").click(function() {
        var object = $(this);
        alert("test-" + object.attr("class").split("test-")[1].split("-"))
    ;});
    

    ​

    More Information

    • jQuery – Attribute Contains Selector
    • jQuery – .attr()
    • jQuery – .click()
    • jsFiddle Demonstration
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am trying to render a haml file in a javascript response like so:
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.