473,468 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Listing of all element's classes

How can I get a list of all the css classes applied to an element?

document.getElementById("someelement");
someelement.?(classlist)... blah

Thanks
Jun 28 '06 #1
5 1198
Brian Herold wrote:
How can I get a list of all the css classes applied to an element?
document.getElementById("someelement");
someelement.?(classlist)... blah


alert(document.getElementById('somelement').classN ame);

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Jun 28 '06 #2
Brian Herold wrote:
How can I get a list of all the css classes applied to an element?

An element only has one className attribute, do you want the styles? If
so, look up getComputedStyle() or for IE, currentStyle.

--
Ian Collins.
Jun 28 '06 #3
Ian Collins wrote:
Brian Herold wrote:
How can I get a list of all the css classes applied to an element?

An element only has one className attribute, do you want the styles? If
so, look up getComputedStyle() or for IE, currentStyle.


I ended up doing:

if(theElement.className.indexOf("somestyle") != -1) {
//code here
}

That way I can check if an element currently has a particular style
applied to it.
Jun 28 '06 #4
Brian Herold wrote:
Ian Collins wrote:
Brian Herold wrote:
How can I get a list of all the css classes applied to an element?

An element only has one className attribute, do you want the styles? If
so, look up getComputedStyle() or for IE, currentStyle.


I ended up doing:

if(theElement.className.indexOf("somestyle") != -1) {
//code here
}

That way I can check if an element currently has a particular style
applied to it.


Unless somestyle is a substring of some other style, in which case you
may get it wrong. Try using a regular expression and match whole
words:

var x = "fred blah tom";
var y = "blahfred blahblah blahtom";
var someClass = 'blah';
var classTest = new RegExp('\\b' + someClass + '\\b');
alert(
'Test: ' + classTest
+ '\n' + someClass + ' in ' + x + '? ' + classTest.test(x)
+ '\n' + someClass + ' in ' + y + '? ' + classTest.test(y)
);
Remember that an alement can have multiple class names in the className
attribute.
--
Rob

Jun 28 '06 #5
RobG wrote:
var classTest = new RegExp('\\b' + someClass + '\\b');


This is insufficient for class names like "a-b" which are, as far as I can
tell, allowed by the specs:
http://www.w3.org/TR/CSS21/syndata.html#q6

"In CSS 2.1, identifiers (including element names, classes, and IDs in
selectors) can contain only the characters [A-Za-z0-9] and ISO 10646
characters U+00A1 and higher, plus the hyphen (-) and the underscore (_);
they cannot start with a digit, or a hyphen followed by a digit."

In my util lib at http://www.javascripttoolbox.com/lib/util/ I use the
following code:

// Determine if an object or class string contains a given class.
css.hasClass = function(obj,className) {
if (!defined(obj) || obj==null || !RegExp) { return false; }
var re = new RegExp("(^|\\s)" + className + "(\\s|$)");
if (typeof(obj)=="string") {
return re.test(obj);
}
else if (typeof(obj)=="object" && obj.className) {
return re.test(obj.className);
}
return false;
};

--
Matt Kruse
http://www.JavascriptToolbox.com
http://www.AjaxToolbox.com
Jun 29 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Alex Sab | last post by:
Hi, I am having trouble describing in a schema that elements can be mandatory in one part and not mandatory in another part of the xml document. Here is a sample xml file <Model:Templates>...
9
by: Derek Basch | last post by:
I have a simple XHTML document that I would like to apply javascript functionality to: <body> <div class="length">9</div> <div class="sequence">AAAAAAATTTAAA</div> <div...
0
by: James Hebben via .NET 247 | last post by:
I am trying to generate C# classes for a specific element in aschema using the XSD.EXE tool. Assume an XSD file that definesan element, Foo, containing an element, Bar. The Bar elementcontains a...
4
by: joewhitehair | last post by:
Using a schema, I created classes for my web service using the XSD.exe tool. With the classes I then wrote a web service function, but the WSDL that is generated has different minOccurs and...
3
by: KK | last post by:
Hello all, I have several classes binded by one common interface - say 'sum' interface which calculates the sum of all the class elements of type 'int'. class Alphabet { int _a; int _b; int...
3
by: ted benedict | last post by:
hi everybody, didn't find this using the search :( this is my problem: i create a dom element dynamically (<span>) and want to assign a class attribute to it such that it has some css style, this...
4
by: TomC | last post by:
Coming from Java, I am looking for something similar to the online Java API pages. I did a search and found a thread from 2003 that listed the following link to an MSDN page....
9
by: Ramashish Baranwal | last post by:
Hi, I want to get a module's contents (classes, functions and variables) in the order in which they are declared. Using dir(module) therefore doesn't work for me as it returns a list in...
4
by: jaime.dyson | last post by:
Hello all, I have the unenviable task of turning about 20K strangely formatted XML documents from different sources into something resembling a clean, standard, uniform format. I like...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.