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.
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.
Share Improve this question edited May 23, 2017 at 12:16 CommunityBot 11 silver badge asked May 22, 2012 at 19:30 sameoldsameold 19.3k22 gold badges65 silver badges88 bronze badges 2-
And what if an element has two classes beginning with
test-
? How will your code know which one you want? – Blazemonger Commented May 22, 2012 at 19:35 - @Blazemonger That won't be a problem. A single element won't have 2 test classes. – sameold Commented May 22, 2012 at 19:40
3 Answers
Reset to default 9Description
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
This should work for you:
var object = this;
var className = object.className.match(/(test-.*?)(?:\s+|$)/)[1];
Class name is the name of the class you are looking for.
If you don't want to use split or regex, you can try having the class in a separate attribute
<span class="someclass test-something" _rel="test-something">test<span>
or
<span class="someclass" _rel="test-something">test<span>
with the script
$("[_rel*='test-']").click(....
And to retrieve the attribute, use $(this).attr("_rel")