473,804 Members | 2,615 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

changing multiple classnames

Let's say we have this:

<div class="some_cla ss some_other_clas s">

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

Jeff
Jul 14 '08
14 1981
RobG wrote:
On Jul 15, 8:45 am, Jeff <jeff@spam_me_n ot.comwrote:
>Joost Diepenmaat wrote:
>>Joost Diepenmaat <jo...@zeekat.n lwrites:
myDiv.classN ame.replace(/\bsome_other_cl ass\b/,"something_els e");
That should ofcourse be:
myDiv.classNa me = myDiv.className .replace(/\bsome_other_cl ass\b/,"something_els e");
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_clas s(\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_cla ss(\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.javas cript:
>Jeff a écrit :
>> Let's say we have this:

<div class="some_cla ss some_other_clas s">

Is it possible to change *one* of the classnames.
<div id="somewhere" class="some_cla ss some_other_clas s">
<script type="text/javascript">
function changeClass(wha t, original, newOne) {
what = document.getEle mentById(what);
what.classNa me = what.className. replace(origina l, newOne);
}
</script>
<button
onclick="chang eClass('somewhe re','some_other _class','new_cl ass')">
change 1 </button>
<button
onclick="chang eClass('somewhe re','some_class ','new_other_cl ass')">
change 2 </button>

This fails here:

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

onclick="change Class('myId','m yClass','myClas s3')">

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.javas cript:

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.javas cript:

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.javas cript:
>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
3514
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 buttons to hide all rows with a particular status. The code to show/hide is relatively easy, but how do I turn them all off at once? Several ideas I had:
5
1672
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 finally settled on was <input type=button...> without an "onClick" attribute (because the purpose of the buttons was only for displaying values). It works fine except they look and act like buttons. No big deal, but is there a better way?
32
3406
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: 12pt} <p class="bold pink medium">bold pink medium sized text</p>
1
2170
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 what the image looks like, and how big it is. the quality is selected using a trackBar1. the MouseUp event of trackBar1 triggers this method public void updatePanel() string file =...
0
347
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 by adding attributes to the XSD itself. These same attributes don't seem to work for class generation however (unless I just did it wrong?) Anway, is there a way for me to specify the classname(s) to be used in the generated code? Thanks in...
7
2245
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 "dname" and then to call the javascript: document.classes.dname.style.display = "none"; This does not work. Can someone suggest me how I could get this to work?
8
3935
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 portion works perfectly, but I would also like to change the background color of the select element with the current focus. I have defined the following classes: ..selected {
8
1497
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 find no mention of the subject.
6
6279
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 are still enquiring). Is it possible to dynamically change the base class to something else? Initial experiments appear to show it is not: -------------------------------- snip -------------------------------- pass
0
9706
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9577
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10325
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10075
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9140
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5519
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4295
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.