Connecting Tech Pros Worldwide Help | Site Map

changing class properties

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 20th, 2005, 10:00 AM
Jeff Thies
Guest
 
Posts: n/a
Default 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

  #2  
Old July 20th, 2005, 10:00 AM
Stuart Palmer
Guest
 
Posts: n/a
Default Re: changing class properties

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" <cyberjeff@sprintmail.com> wrote in message
news:3F56D5FA.687D3D7F@sprintmail.com...[color=blue]
> 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[/color]


  #3  
Old July 20th, 2005, 10:00 AM
Jeff Thies
Guest
 
Posts: n/a
Default Re: changing class properties

Hi Stuart.

Stuart Palmer wrote:[color=blue]
>
> what are you trying to do this for? links?different browsers?[/color]

IE5+ maybe NS6
[color=blue]
> there may be a
> better way to do it than using JS to dynamicaly change it[/color]

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.styleSheets['a-stylesheet'].href='stylesheet_with_different_class_properties' ;

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

[color=blue]
>
> Stu
>
> "Jeff Thies" <cyberjeff@sprintmail.com> wrote in message
> news:3F56D5FA.687D3D7F@sprintmail.com...[color=green]
> > 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[/color][/color]
  #4  
Old July 20th, 2005, 10:00 AM
Richard Cornford
Guest
 
Posts: n/a
Default Re: changing class properties

"Jeff Thies" <cyberjeff@sprintmail.com> wrote in message
news:3F56D5FA.687D3D7F@sprintmail.com...[color=blue]
> 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?[/color]

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.styleSheets 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.styleSheets 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.styleSheets){
var sheet = document.styleSheets[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.styleSheets 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.


  #5  
Old July 20th, 2005, 10:00 AM
Jeff Thies
Guest
 
Posts: n/a
Default Re: changing class properties

> > there may be a[color=blue][color=green]
> > better way to do it than using JS to dynamicaly change it[/color]
>
> I have form fields that I want to turn on/off (display: none) depending
> on the value of a rolldown.[/color]

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,class_style,style_valu e){
var style_sheet=document.styleSheets[s_s];
for (var j=style_sheet.rules.length-1;j>=0;j--){
var sS=style_sheet.rules[j];
var class_name=sS.selectorText.replace(/\./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="javascript:void(0)"
onclick="setClassStyle(0,'a','color','red')">make red</a><br />
<a href="javascript:void(0)"
onclick="setClassStyle(0,'a','color','orange')">ma ke orange</a>
<p class="a">some class a text is here</p>

</body>
</html>

Cheers,
Jeff











[color=blue]
>
> 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.styleSheets['a-stylesheet'].href='stylesheet_with_different_class_properties' ;
>
> 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
>[color=green]
> >
> > Stu
> >
> > "Jeff Thies" <cyberjeff@sprintmail.com> wrote in message
> > news:3F56D5FA.687D3D7F@sprintmail.com...[color=darkred]
> > > 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[/color][/color][/color]
  #6  
Old July 20th, 2005, 10:01 AM
Jeff Thies
Guest
 
Posts: n/a
Default Re: changing class properties

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(sheet_index,classname,class_style,st yle_value){
var style_sheet=document.styleSheets[sheet_index];
if(!style_sheet){return;}
var sRules = style_sheet.cssRules || style_sheet.rules;
for (var j=sRules.length-1;j>=0;j--){ // later rules will overide //
earlier
var sS=sRules[j];
var class_name=sS.selectorText.replace(/\./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
[color=blue][color=green]
> > 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?[/color]
>
> 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.styleSheets 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.styleSheets 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.styleSheets){
> var sheet = document.styleSheets[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.styleSheets 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.[/color]
  #7  
Old July 20th, 2005, 10:01 AM
Richard Cornford
Guest
 
Posts: n/a
Default Re: changing class properties

"Jeff Thies" <cyberjeff@sprintmail.com> wrote in message
news:3F56E980.F0FE2648@sprintmail.com...
<snip>[color=blue]
> for (var j=sRules.length-1;j>=0;j--){[/color]
<snip>

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

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

Richard.


 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.