473,387 Members | 1,548 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

adding a second class name to an element

Occasionally, in creating a static page I have assigned two classes to
an element. For example: "class = 'BodyText Note'." It's a neat way
of streamlining my stylesheets.

However, I have never been able to use JavaScript to dynamically add a
second class to an element. I'm guessing that this involves some
string concatenation, and I must be making some silly mistake. The
type of approach I've tried has been:
theElement.className = 'AClass' + ' ' + 'AnotherClass'.

Jeff
Firefox Rules
Jul 23 '05 #1
2 2075


Jeff Gutsell wrote:

However, I have never been able to use JavaScript to dynamically add a
second class to an element. I'm guessing that this involves some
string concatenation, and I must be making some silly mistake. The
type of approach I've tried has been:
theElement.className = 'AClass' + ' ' + 'AnotherClass'.


That should work (if theElement is a reference to an HTML element object).
The only problem is that Mozilla/Firefox currently have a bug where
theElement.className = 'Class1 ';
theElement.className += 'Class2';
results in className being 'Class1Class2' but your assignment above
should still work.
The bug report is here:
https://bugzilla.mozilla.org/show_bug.cgi?id=254337
--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2
Martin Honnen wrote:
Jeff Gutsell wrote:
However, I have never been able to use JavaScript to dynamically add a
second class to an element. I'm guessing that this involves some
string concatenation, and I must be making some silly mistake. The
type of approach I've tried has been:
theElement.className = 'AClass' + ' ' + 'AnotherClass'.
That should work (if theElement is a reference to an HTML element object).


And the more reasonable

theElement.className = 'AClass AnotherClass'.

also works at least in Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.7.5)
Gecko/20041128 Firefox/1.0 (Debian package 1.0-4).
The only problem is that Mozilla/Firefox currently have a bug where
theElement.className = 'Class1 ';
theElement.className += 'Class2';
results in className being 'Class1Class2' [...]


Well, I do not consider this a bug, rather a misconception. Leading and
trailing whitespace should be removed from the value after assignment and
on retrieval (as it serves no greater purpose in the DOM). Note that

a.b += c;

is a shortcut for

a.b = a.b + c;
,---------^
(that includes reading the property)

So the only proper way to append a style class reference should be

theElement.className = 'Class1';
...
theElement.className += ' Class2';

which works, as described in Bugzilla.
PointedEars
Jul 23 '05 #3

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

Similar topics

12
by: Anna | last post by:
Hi all, I posted the same question this afternoon but my message isn't showing up, so I thought I'd give it another try.... in case you should see it later I apologize for posting the same...
15
by: crjunk | last post by:
I have 4 TextBoxes on my form that I'm trying to add together to get a grand total. Here is the code I'm using: <SCRIPT LANGUAGE="JavaScript"> <!-- Beginning of JavaScript - function...
0
by: keith bannister via .NET 247 | last post by:
(Type your message here) -------------------------------- From: keith bannister Hi, I'm new to .net (as of last week) but here goes. I want to serialize/deserialize a file the conforms...
1
by: Todd | last post by:
I have the following code that inserts a new element and its attributes into an xml file. The problem is the syntax of the inserted row. here is my code: 'Create an XmlDataDocument. Dim doc As...
4
by: glebur | last post by:
Hi, I'm trying to create a web service client in C# but I get stuck at one of the first steps. When adding a Web reference to the Visual Studio project; I get this error (this is a translation,...
2
by: Muzzy | last post by:
Hi, I've used information on these newsgroups to build many pages. So I thought that now that I have my script working (something that I've been working on for about a week), I should post it so...
5
by: Water Cooler v2 | last post by:
I know that we can add a single name value entry in app.config or web.config in the configuration/configSettings/appSettings section like so: <add key="key" value="value" /> Question: I want...
1
by: The Eclectic Electric | last post by:
I'd be very grateful if anyone could help me with this. From my limited knowledge of Javascript I don't think it is possible, but I'll punt anyway. I downloaded and very slightly adapted this...
5
by: Gustaf | last post by:
Here's what happens: I got a Web Service installed locally on my IIS. Then I got a client project, which will call this service. But when I add the Web Reference to the Web Service, and look at the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...

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.