473,792 Members | 2,877 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 #1
14 1979
Jeff <jeff@spam_me_n ot.comwrites:
Let's say we have this:

<div class="some_cla ss some_other_clas s">

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

myDiv.className .replace(/\bsome_other_cl ass\b/,"something_els e");

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

myDiv.className = myDiv.className .replace(/\bsome_other_cl ass\b/,"something_els e");

--
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_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.className = what.className. replace(origina l, newOne);
}
</script>
<button onclick="change Class('somewher e','some_other_ class','new_cla ss')">
change 1
</button>
<button onclick="change Class('somewher e','some_class' ,'new_other_cla ss')">
change 2
</button>

--
sm
Jul 14 '08 #4
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.className = what.className. replace(origina l, newOne);
}
</script>
<button
onclick="change Class('somewher e','some_other_ class','new_cla ss')">
change 1 </button>
<button
onclick="change Class('somewher e','some_class' ,'new_other_cla ss')">
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.

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
Jul 14 '08 #5
"Evertjan." <ex************ **@interxnl.net writes:
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.n lwrites:
>myDiv.classNam e.replace(/\bsome_other_cl ass\b/,"something_els e");

That should ofcourse be:

myDiv.className = myDiv.className .replace(/\bsome_other_cl ass\b/,"something_els e");
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_n ot.comwrote:
Joost Diepenmaat wrote:
Joost Diepenmaat <jo...@zeekat.n lwrites:
myDiv.className .replace(/\bsome_other_cl ass\b/,"something_els e");
That should ofcourse be:
myDiv.className = 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 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. 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.javas cript:
"Evertjan." <ex************ **@interxnl.net writes:
>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="largeFon t submitButton"he lps
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

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
3404
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
2167
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
3930
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
6277
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
9518
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
10430
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10211
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...
1
10159
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10000
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
9033
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...
1
7538
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5436
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...
3
2917
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.