Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Changing style of a class

Status
Not open for further replies.

jl280328

Programmer
Jun 15, 2006
97
US
I have a class named RetentionAR. It is defined in the style tags on the webpage itself. When my webpage loads the display of the class is set to the default. I was wondering if there is a way in javascript to set this class to none. There are about 180 items with this class name.
 
I'm not sure I understand correctly. Do you want to set each element on the page with a class of RetentionAR to have no css class?

If that is the case then here's an example. Unfortunately there's no getElementsByClassName function so we'll have to sniff them out manually. If you know what type of tag this class is applied to it will speed up things considerably because you can search thru only those tags. Try something like this:
Code:
[gray][i]//this example assumes that the class has been applied to input elements[/i][/gray]
function removeClass() {
   var objs = document.getElementsByName("input");
   for (i = 0; i < objs.length; i++) {
      if (objs[i].className == "RetentionAR") {
         objs[i].className = "";
      }
   }
}



-kaht

[small](All puppies have now found loving homes, thanks for all who showed interest)[/small]
 
i would suggest replacing the instance of the class with a blank string, rather than just setting the className to "".

a class such as "big red ugly", for example. when looking to replace "red", surely you'd want "big ugly" and not "".



*cLFlaVA
----------------------------
[tt]"quote goes here"[/tt]
[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
I did this that way you don't have to loop through every element since I have around 200 of them
Code:
for (var LoopIndex1 = 0; LoopIndex1 < document.styleSheets [0].rules.length; LoopIndex1++)
{
if (document.styleSheets [0].rules [LoopIndex1].selectorText.toUpperCase () == '.RETENTIONAR')
{
document.styleSheets [0].rules [LoopIndex1].style.visibility='hidden' 
}
}
 
A quick fix would be ... this script :
Code:
function blankRetentionAR() {
  var css = document.createElement('link'); 
  css.rel = 'stylesheet'; 
  css.type = 'text/css'; 
  css.href = 'blankRetentionAR.css'; 
  document.body.appendChild(css);
}

... to load this stylesheet, named blankRetentionAR.css:

Code:
.RetentionAR {
}

;-)
 
FYI - calling the script above will override the class style dynamically...

If you wanna try it out, have a look at my FAQ on it : faq216-6458


;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top