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

Home Posts Topics Members FAQ

changing class properties

I have a number of elements of "some-class".

I'd like to change the styles of some-class:

from

..some-class{color: red; display: block}

to

..some-class{color: red; display: none}

How do I do that?

Cheers,
Jeff
Jul 20 '05 #1
6 10345
what are you trying to do this for? links?different browsers? there may be a
better way to do it than using JS to dynamicaly change it

Stu

"Jeff Thies" <cy*******@spri ntmail.com> wrote in message
news:3F******** *******@sprintm ail.com...
I have a number of elements of "some-class".

I'd like to change the styles of some-class:

from

.some-class{color: red; display: block}

to

.some-class{color: red; display: none}

How do I do that?

Cheers,
Jeff

Jul 20 '05 #2
Hi Stuart.

Stuart Palmer wrote:

what are you trying to do this for? links?different browsers?
IE5+ maybe NS6
there may be a
better way to do it than using JS to dynamicaly change it
I have form fields that I want to turn on/off (display: none) depending
on the value of a rolldown.

I could give ID's to all them and iterate through a list of them, seems
awkward.

I can change between the href of a stylesheet, so it can choose an
external sheet that has the right class properties:

document.styleS heets['a-stylesheet'].href='styleshe et_with_differe nt_class_proper ties';

But it seems to me that I should be able to rewrite the rule in the
stylesheet, I don't remember how... That would seem to be the most
maintainable.

Cheers,
Jeff


Stu

"Jeff Thies" <cy*******@spri ntmail.com> wrote in message
news:3F******** *******@sprintm ail.com...
I have a number of elements of "some-class".

I'd like to change the styles of some-class:

from

.some-class{color: red; display: block}

to

.some-class{color: red; display: none}

How do I do that?

Cheers,
Jeff

Jul 20 '05 #3
"Jeff Thies" <cy*******@spri ntmail.com> wrote in message
news:3F******** *******@sprintm ail.com...
I have a number of elements of "some-class".

I'd like to change the styles of some-class:

from

.some-class{color: red; display: block}

to

.some-class{color: red; display: none}

How do I do that?


It is probably tackling the problem form the wrong end, but if you
insist on modifying the class definition rather than switching the class
on the elements you will probably need to directly modify the
documents.style Sheets object (where supported).

Unfortunately there are two "types" of styleSheet object, the W3C DOM
version (Mozilla, of course) and the IE version. With a number of
browsers implementing both. Fortunately they are sufficiently similar in
structure to be relatively easily worked with.

The document.styleS heets object is a collection with indexed members (IE
also supports members named by ID attribute but sticking to the indexes
is more cross browser). So the first styleSheet is:-

if(document.sty leSheets){
var sheet = document.styleS heets[0]
...
}

Each styleSheet contains a collection of style rules. On the W3C version
the collection is called "cssRules" and on IE it is called "rules", so:-

if(sheet){
var ssRules = sheet.cssRules || sheet.rules;
...
}

The rules are an indexed collection of style rules. I would not be
inclined to rely on the order (though they may follow the order in the
CSS on all implementations , but that would be an assumption). So that
would probably involve looping through the rules list looking for the
required item.

if(sheet){
var result = null;
for(var c = 0; c < ssRules.length; c++){
if(ssRules[c].selectorText == ".some-class"){
result = ssRules[c].style;
break;
}
}
...
}

Having identified the rule that has the desired - selectorText - value
that rule has a - style - object (virtually identical to normal
elements - style - objects) on which the desired property can be set:-

if(result){
result.display = "none"; //This object could be cached
//so that subsequent changes
//could avoid going through
//the whole retrieval process.
...
}

Of the major browsers Opera 7.11 currently does not implement a
document.styleS heets object and you won't get much joy out of Netscape 4
with this method (not that switching the display property works there
anyway) so it must be handled cautiously to avoid errors.

Another approach that might prove fruitful could be to declare two
separate STYLE elements and set the - disabled - attribute on one of
them. Later using a script to switch the initially disabled STYLE
element to enabled and disable the other. I haven't tried that myself
but I have seen it done in order to switch between multiple external
style sheets and it did work with Opera 7.

Richard.
Jul 20 '05 #4
> > there may be a
better way to do it than using JS to dynamicaly change it
I have form fields that I want to turn on/off (display: none) depending
on the value of a rolldown.


This is a bit awkward but it works in IE5:
I can't get it to use named stylesheets. Isn't there a better way to
quit a loop than return 0?

<html>
<head>
<script type="text/javascript">
function setClassStyle(s _s,classname,cl ass_style,style _value){
var style_sheet=doc ument.styleShee ts[s_s];
for (var j=style_sheet.r ules.length-1;j>=0;j--){
var sS=style_sheet. rules[j];
var class_name=sS.s electorText.rep lace(/\./g,'');
if(class_name== classname){
sS.style[class_style]=style_value;
return 0;
}
}}
</script>
<style type="text/css" name="s">
..a{color: blue}
..b{color: orange}
</style>
</head>
<body>
<a href="javascrip t:void(0)"
onclick="setCla ssStyle(0,'a',' color','red')"> make red</a><br />
<a href="javascrip t:void(0)"
onclick="setCla ssStyle(0,'a',' color','orange' )">make orange</a>
<p class="a">some class a text is here</p>

</body>
</html>

Cheers,
Jeff




I could give ID's to all them and iterate through a list of them, seems
awkward.

I can change between the href of a stylesheet, so it can choose an
external sheet that has the right class properties:

document.styleS heets['a-stylesheet'].href='styleshe et_with_differe nt_class_proper ties';

But it seems to me that I should be able to rewrite the rule in the
stylesheet, I don't remember how... That would seem to be the most
maintainable.

Cheers,
Jeff

Stu

"Jeff Thies" <cy*******@spri ntmail.com> wrote in message
news:3F******** *******@sprintm ail.com...
I have a number of elements of "some-class".

I'd like to change the styles of some-class:

from

.some-class{color: red; display: block}

to

.some-class{color: red; display: none}

How do I do that?

Cheers,
Jeff

Jul 20 '05 #5
Thanks Richard,

You answered every question I had and a few that hadn't occured to me.

Here's what I'll probably use:

function setClassStyle(s heet_index,clas sname,class_sty le,style_value) {
var style_sheet=doc ument.styleShee ts[sheet_index];
if(!style_sheet ){return;}
var sRules = style_sheet.css Rules || style_sheet.rul es;
for (var j=sRules.length-1;j>=0;j--){ // later rules will overide //
earlier
var sS=sRules[j];
var class_name=sS.s electorText.rep lace(/\./g,'');
if(class_name== classname){
sS.style[class_style]=style_value;
break;
}
}}
I'm not going to worry about Opera 7 for this as there will be only a
handfull of IE users. My guess is that my client is going to want to
change how he want these forms to change and I thought classname changes
would have been the easiest to switch.

Thanks!
Jeff
I have a number of elements of "some-class".

I'd like to change the styles of some-class:

from

.some-class{color: red; display: block}

to

.some-class{color: red; display: none}

How do I do that?


It is probably tackling the problem form the wrong end, but if you
insist on modifying the class definition rather than switching the class
on the elements you will probably need to directly modify the
documents.style Sheets object (where supported).

Unfortunately there are two "types" of styleSheet object, the W3C DOM
version (Mozilla, of course) and the IE version. With a number of
browsers implementing both. Fortunately they are sufficiently similar in
structure to be relatively easily worked with.

The document.styleS heets object is a collection with indexed members (IE
also supports members named by ID attribute but sticking to the indexes
is more cross browser). So the first styleSheet is:-

if(document.sty leSheets){
var sheet = document.styleS heets[0]
...
}

Each styleSheet contains a collection of style rules. On the W3C version
the collection is called "cssRules" and on IE it is called "rules", so:-

if(sheet){
var ssRules = sheet.cssRules || sheet.rules;
...
}

The rules are an indexed collection of style rules. I would not be
inclined to rely on the order (though they may follow the order in the
CSS on all implementations , but that would be an assumption). So that
would probably involve looping through the rules list looking for the
required item.

if(sheet){
var result = null;
for(var c = 0; c < ssRules.length; c++){
if(ssRules[c].selectorText == ".some-class"){
result = ssRules[c].style;
break;
}
}
...
}

Having identified the rule that has the desired - selectorText - value
that rule has a - style - object (virtually identical to normal
elements - style - objects) on which the desired property can be set:-

if(result){
result.display = "none"; //This object could be cached
//so that subsequent changes
//could avoid going through
//the whole retrieval process.
...
}

Of the major browsers Opera 7.11 currently does not implement a
document.styleS heets object and you won't get much joy out of Netscape 4
with this method (not that switching the display property works there
anyway) so it must be handled cautiously to avoid errors.

Another approach that might prove fruitful could be to declare two
separate STYLE elements and set the - disabled - attribute on one of
them. Later using a script to switch the initially disabled STYLE
element to enabled and disable the other. I haven't tried that myself
but I have seen it done in order to switch between multiple external
style sheets and it did work with Opera 7.

Richard.

Jul 20 '05 #6
"Jeff Thies" <cy*******@spri ntmail.com> wrote in message
news:3F******** *******@sprintm ail.com...
<snip>
for (var j=sRules.length-1;j>=0;j--){

<snip>

An alternative - for - loop that counts down through an array-like
collection:-

for(var j = sRules.length ; j-- ; ){
...
}

Richard.
Jul 20 '05 #7

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

Similar topics

2
1763
by: Pawe³ | last post by:
Is there any way I can dynamically (taking advantage of JavaScript) change the properties for classes defined in CSS styles?? Let say there's a CSS class ".regtex" and it forces the browser to display all elements of that class in red. Now I want to change this class so all elements belonging to this class are displayed in blue, however I don't want to change every single element, I just want to change the class' properties. Is it...
1
3082
by: Rolln_Thndr | last post by:
I'm vey new to network programing and have a few rather fundemental questions. I'm creating a very basic UDP proxy server and having a few issues regarding the sockets. Is it possible to change the properties of a socket WITHOUT closing it and creating a new one? Basically, I need to change the port of a bound socket, but the only way I have found to do this so far is to close the exisitng socket and create a new one. This in itself...
1
3382
by: Jaweed | last post by:
Hi I'm developing an ASP.NET app in Visual studio (using C# for code behind). My question is fairly simple (i think). When I go to the code behind page, the controls in the html page are made "protected" so that it can only be accessed from within that page. I would like to permanently make this "public" for certain controls, so that I can access them from another page (the page that a user control is embedded into for example). If I...
4
2449
by: Tony W | last post by:
Hi, I am trying to write a simple application to retrieve data from the Windows registry and insert it into textboxs on a windows form. So far I have one namespace containing two classess. The first class handles the form generation - (this was done using GUI form designer).
7
2978
by: Sakharam Phapale | last post by:
Hi All, How to preserve the old font properties while changing new one? I posted same question 2 months back, but I had very small time then. eg. "Shopping for" is a text in RichTextBox and already formatted as "Shopping" ---Bold "for" -----Regular Now I want to underline whole text by preserving old style i.e. Bold and
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?
7
1492
by: Brett Romero | last post by:
I'd like to copy a object1 into object2 so object2 can be manipulated. Object1 is coming form the middle layer into the UI layer. I'd like to rename a field in Object2 from "somethingID" to just "ID" and do this several times for a few middle layer objects. This will allow me to create generic<> lists and reference the field ID against many object2s that are similar to object1. The middle layer objects have ID fields with different...
4
1843
by: andychambers2002 | last post by:
I'm working on a "TempFile" class that stores the data in memory until it gets larger than a specified threshold (as per PEP 42). Whilst trying to implement it, I've come across some strange behaviour. Can anyone explain this? The test case at the bottom starts a TempFile at size 50 and prints its type. It then increases the size to the threshold at which point "self" is changed to being a TemporaryFile. It seems that the next call...
4
3306
by: Tugrul HELVACI | last post by:
Changing DisplayNames of my properties using PropertyGrid component, how ?? I'm using Delphi 2006 and I have a class defination like this: TPerson = class fPersonName : String; fPersonSurName : String; fPersonAge : Integer; published property PersonName : String read fPersonName write fPersonName;
1
1521
by: pigeonrandle | last post by:
Hi, I thought i should give a better explanation to my second 'bug' in my previous post (Bugs in VS2003 SP1) because it's taken me ages to figure out what was going on. Basically, one of my project classes 'Tokenizer' had a property '_Token'. When i hovered my mouse over a line of code with 'Tokenizer._Token' in it, i saw the value i expected. If i entered 'Tokenizer._Token' into the watch window i saw the value i expected.
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
10569
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...
1
10315
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,...
1
7615
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
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.