473,396 Members | 1,785 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,396 software developers and data experts.

changing multiple classnames

Let's say we have this:

<div class="some_class some_other_class">

Is it possible to change *one* of the classnames.

Jeff
Jul 14 '08 #1
14 1940
Jeff <jeff@spam_me_not.comwrites:
Let's say we have this:

<div class="some_class some_other_class">

Is it possible to change *one* of the classnames.
If that's a question: yes.

myDiv.className.replace(/\bsome_other_class\b/,"something_else");

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Jul 14 '08 #2
Joost Diepenmaat <jo***@zeekat.nlwrites:
myDiv.className.replace(/\bsome_other_class\b/,"something_else");
That should ofcourse be:

myDiv.className = myDiv.className.replace(/\bsome_other_class\b/,"something_else");

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Jul 14 '08 #3
SAM
Jeff a écrit :
Let's say we have this:

<div class="some_class some_other_class">

Is it possible to change *one* of the classnames.
<div id="somewhere" class="some_class some_other_class">
<script type="text/javascript">
function changeClass(what, original, newOne) {
what = document.getElementById(what);
what.className = what.className.replace(original, newOne);
}
</script>
<button onclick="changeClass('somewhere','some_other_class ','new_class')">
change 1
</button>
<button onclick="changeClass('somewhere','some_class','new _other_class')">
change 2
</button>

--
sm
Jul 14 '08 #4
SAM wrote on 15 jul 2008 in comp.lang.javascript:
Jeff a écrit :
> Let's say we have this:

<div class="some_class some_other_class">

Is it possible to change *one* of the classnames.

<div id="somewhere" class="some_class some_other_class">
<script type="text/javascript">
function changeClass(what, original, newOne) {
what = document.getElementById(what);
what.className = what.className.replace(original, newOne);
}
</script>
<button
onclick="changeClass('somewhere','some_other_class ','new_class')">
change 1 </button>
<button
onclick="changeClass('somewhere','some_class','new _other_class')">
change 2 </button>
This fails here:

<div class='myClass2 myClass' id='myId'>

onclick="changeClass('myId','myClass','myClass3')" >

The regex replace with word boundaries is safer.

======

My idea is that the whole multiple class method is error prone,
I never use it anymore.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 14 '08 #5
"Evertjan." <ex**************@interxnl.netwrites:
My idea is that the whole multiple class method is error prone,
it is
I never use it anymore.
but it can be very useful.

--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
Jul 14 '08 #6
Joost Diepenmaat wrote:
Joost Diepenmaat <jo***@zeekat.nlwrites:
>myDiv.className.replace(/\bsome_other_class\b/,"something_else");

That should ofcourse be:

myDiv.className = myDiv.className.replace(/\bsome_other_class\b/,"something_else");
Perfect!

As a former Perl guy, any time you can use a regex is a good time!

Jeff
Jul 14 '08 #7
On Jul 15, 8:45 am, Jeff <jeff@spam_me_not.comwrote:
Joost Diepenmaat wrote:
Joost Diepenmaat <jo...@zeekat.nlwrites:
myDiv.className.replace(/\bsome_other_class\b/,"something_else");
That should ofcourse be:
myDiv.className = myDiv.className.replace(/\bsome_other_class\b/,"something_else");

Perfect!
No, it's not. The class attribute value delimiter is one or more
whitespace characters, so a pattern should be used that matches that.
\b matches a word break which includes whitespace, but there are
characters that a javascript regexp recognises as a word break that
are not designated as class attribute value delimiters, such as hyphen
(-) and period (.). A better match is (^|\s)some_class(\s|$).

That will also remove some or all of the whitespace around the value
when replacing it, so pad the replacement string with spaces:

myDiv.className =
myDiv.className.replace(/(^|\s+)some_class(\s+|$)/,"
something_else ");
Extra whitespace may now be inserted in the class attribute that some
browsers will not automatically trim. If you want, you can follow it
up with a trimming function that removes leading and trailing
whitespace, the \s+ should ensure there is no extra internal
whitespace.

The additional whitespace shouldn't be an issue unless you are doing
lots (hundreds?) of replacements for a particular element without re-
loading the page, however I think it is better to deal with it from
the start, so append:

.replace(/^\s+|\s+$/g,'')
or whatever your favourite trimming pattern is.
--
Rob
Jul 15 '08 #8
Joost Diepenmaat wrote on 15 jul 2008 in comp.lang.javascript:
"Evertjan." <ex**************@interxnl.netwrites:
>My idea is that the whole multiple class method is error prone,

it is
>I never use it anymore.

but it can be very useful.
Many dangerous things are,
otherwise their danger would not be important.

So, don't use it in a complex situation.
And why use it in a simple one, where you can easily do without?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 15 '08 #9
Evertjan. meinte:
My idea is that the whole multiple class method is error prone,
I never use it anymore.
Why? Never experienced any "errors" due to multiple classes. On the
contrary it is in some cases *extremely* useful. Something like <td
class="checkSum error"or <input class="largeFont submitButton"helps
to keep the CSS short and flexible. I use it extensively with my
dynamically created JS widgets to "attach" extra information to DOM
elements, too.

Gregor
--
http://photo.gregorkofler.at ::: Landschafts- und Reisefotografie
http://web.gregorkofler.com ::: meine JS-Spielwiese
http://www.image2d.com ::: Bildagentur für den alpinen Raum
Jul 15 '08 #10
RobG wrote:
On Jul 15, 8:45 am, Jeff <jeff@spam_me_not.comwrote:
>Joost Diepenmaat wrote:
>>Joost Diepenmaat <jo...@zeekat.nlwrites:
myDiv.className.replace(/\bsome_other_class\b/,"something_else");
That should ofcourse be:
myDiv.className = myDiv.className.replace(/\bsome_other_class\b/,"something_else");
Perfect!

No, it's not. The class attribute value delimiter is one or more
whitespace characters, so a pattern should be used that matches that.
\b matches a word break which includes whitespace, but there are
characters that a javascript regexp recognises as a word break that
are not designated as class attribute value delimiters, such as hyphen
(-) and period (.). A better match is (^|\s)some_class(\s|$).
That's a good point. It's just occurred to me that I've never used \b
and hadn't thought of it matching "-", nor did I know you could do this:
(^|\s), I probably would have done \s*
>
That will also remove some or all of the whitespace around the value
when replacing it, so pad the replacement string with spaces:

myDiv.className =
myDiv.className.replace(/(^|\s+)some_class(\s+|$)/,"
something_else ");
Extra whitespace may now be inserted in the class attribute that some
browsers will not automatically trim.
Is there a problem with doing this?:

myDiv.className.replace(/\s*some_class\s*/," something_else ")
If you want, you can follow it
up with a trimming function that removes leading and trailing
whitespace, the \s+ should ensure there is no extra internal
whitespace.

The additional whitespace shouldn't be an issue unless you are doing
lots (hundreds?) of replacements for a particular element without re-
loading the page,
Hmmm, is there a practical character limit for the classname attribute?

Jeff
however I think it is better to deal with it from
the start, so append:

.replace(/^\s+|\s+$/g,'')
or whatever your favourite trimming pattern is.
--
Rob
Jul 15 '08 #11
Evertjan. wrote:
SAM wrote on 15 jul 2008 in comp.lang.javascript:
>Jeff a écrit :
>> Let's say we have this:

<div class="some_class some_other_class">

Is it possible to change *one* of the classnames.
<div id="somewhere" class="some_class some_other_class">
<script type="text/javascript">
function changeClass(what, original, newOne) {
what = document.getElementById(what);
what.className = what.className.replace(original, newOne);
}
</script>
<button
onclick="changeClass('somewhere','some_other_clas s','new_class')">
change 1 </button>
<button
onclick="changeClass('somewhere','some_class','ne w_other_class')">
change 2 </button>

This fails here:

<div class='myClass2 myClass' id='myId'>

onclick="changeClass('myId','myClass','myClass3')" >

The regex replace with word boundaries is safer.

======

My idea is that the whole multiple class method is error prone,
I never use it anymore.
What errors would that be?

I rarely use multiple classes, but for somethings they are perfect.

If your classes are doing completely different things, without
overlapping properties it can be a neat trick. Particularly if you can
simplify the html by doing so (in my case avoiding a wrapper div).

I personally like having as few classes as possible, I think we've
all seen stylesheets that were loaded down.

Jeff
>
Jul 15 '08 #12
Jeff wrote on 15 jul 2008 in comp.lang.javascript:

I wrote:
>My idea is that the whole multiple class method is error prone,
I never use it anymore.

What errors would that be?

I rarely use multiple classes, but for somethings they are perfect.

If your classes are doing completely different things, without
overlapping properties it can be a neat trick. Particularly if you can
simplify the html by doing so (in my case avoiding a wrapper div).
You answer yourself "If ...."

That is the general answer to "error prone":
"Bit if you make no mistakes, ...."

The thread was about dynamicly changing one class in an multiple class
element. Here the chances of an error multiply.
>
I personally like having as few classes as possible, I think we've
all seen stylesheets that were loaded down.
Does that contracivt my stand?

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 15 '08 #13
Evertjan. wrote:
Jeff wrote on 15 jul 2008 in comp.lang.javascript:

I wrote:
>>My idea is that the whole multiple class method is error prone,
I never use it anymore.
What errors would that be?

I rarely use multiple classes, but for somethings they are perfect.

If your classes are doing completely different things, without
overlapping properties it can be a neat trick. Particularly if you can
simplify the html by doing so (in my case avoiding a wrapper div).

You answer yourself "If ...."

That is the general answer to "error prone":
"Bit if you make no mistakes, ...."

The thread was about dynamicly changing one class in an multiple class
element. Here the chances of an error multiply.
Well, in my mind there is a difference between an error and creating a
mess. I can certainly see how this could be a mess, I don't see how it
would throw an error. Unless I misunderstand, I'd like to know.
>
> I personally like having as few classes as possible, I think we've
all seen stylesheets that were loaded down.

Does that contracivt my stand?
Not at all...
I'm not trying to be argumentative, if there's something I'm missing I'd
like to know. If we just have a symantic mixup... Or perhaps you are
referring to your regex not matching what you expect? Or one class
stepping on another's definitions and not yielding the correct styles?
The more I think of this, I think that's what you must mean.

I'm still pretty happy as it never occurred to me that className
would refer to the whole string, I was thinking it might be in an array,
or not available at all! For my case, it eliminates the need for a
wrapper div, I might never need it again.
Jeff
>

Jul 15 '08 #14
Jeff wrote on 15 jul 2008 in comp.lang.javascript:
>The thread was about dynamicly changing one class in an multiple class
element. Here the chances of an error multiply.

Well, in my mind there is a difference between an error and creating a
mess. I can certainly see how this could be a mess, I don't see how it
would throw an error. Unless I misunderstand, I'd like to know.
"throw an error"?

"error prone" is not confined to a execution errors,
you can also make errors in programming.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 15 '08 #15

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

Similar topics

2
by: RIck Measham | last post by:
I have a dynamically generated table, filled from a database by a perl application. Each row represents a database record and has a 'status' and a unique 'id'. What I want to do is create...
5
by: martin | last post by:
I needed a way to display calculated, multiple, changing values (numerical sums) as users interacted with the page, and do this without going back to the server to load the page again. What I...
32
by: Will Hartung | last post by:
Can someone clarify that multiple classes in the "class" attribute are ok and "legal" and not some fluke? So, I can do: ..pink {color: pink} ..bold {font-weight: bold} ..medium {font-size:...
1
by: Sean | last post by:
i am writing a class that visual demonstrates the result of changing the quality of a jpeg. the intention is to do this by saving the file and a selected quality, then reloading it to get an idea of...
0
by: Bill Cohagan | last post by:
Is there a way to control generated classnames when generating classes from an XSD using xsd.exe? When generating typed datasets it's possible to provide some direction via the "codegen" namespace...
7
by: Wim Roffil | last post by:
I have a simple webpage (without stylesheet) where I want to be able to switch some parts of the page off. I tried to do this by giving all the elements that should be switched off the class...
8
by: Bosconian | last post by:
I have two multiple select inputs. Initially the first contains a bunch of items and the second is empty. Using a common method, I move items back and forth by double clicking on them. This...
8
by: mscertified | last post by:
I have two stylesheet files, one is a standard file and one a custon file. They have no classnames in common. How do I specify that my page use both stylesheets? I tried Help and as usual could...
6
by: Adam C. | last post by:
We have a situation where we want a Swig-generated Python class to have a different base (not object). It doesn't appear that we can coerce Swig into generating the class we want at present (but we...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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,...

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.