473,326 Members | 2,128 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

get elements by class name and change its style?

20
Hello,

Here's the challenge I'm trying to solve:

I'd like, when I click a button on a page, that it causes the style for all elements of a certain class to change. When I click the button again, it should change back. Using getElementsById would be inefficient because I don't want to assign a specific ID to every single element on my page. Here's a simplified example:

[HTML]
<p class="normal">Here is some text that will not change</p>
<p class="changeable">This is text that will be changed</p>
<p class="normal">More text that won't change</p>
<p class="changeable">More text that will be changed</p>
<p class="normal">Again, more unchanging text</p>
<p class="changeable">More text that will be changed</p>
<a href="#" onclick="function to change the style of the p class changeable">
[/HTML]

Any suggestions?

Thanks!
Oct 24 '07 #1
4 23442
TAL651
20
I've found a script that works in Firefox and Safari, but not in IE. Here is the example html and the javascript; I would really appreciate anyone being able to point out what prevents this from working in IE:

Expand|Select|Wrap|Line Numbers
  1. function changeStyles()
  2. {
  3.    var ss = document.styleSheets;
  4.    for (i=0;i<ss[0].cssRules.length;i++)
  5.    {
  6.      if (  ss[0].cssRules[i].selectorText.indexOf(".changeable") > -1 )
  7.      {
  8.        ss[0].deleteRule(i);
  9.        ss[0].insertRule(".changeable { color: red; }", i);
  10.      }
  11.    }
  12. }
  13.  
  14.  
[HTML]
<p class="normal">Here is some text that will not change</p>
<p class="changeable">This is text that will be changed</p>
<p class="normal">More text that won't change</p>
<p class="changeable">More text that will be changed</p>
<p class="normal">Again, more unchanging text</p>
<p class="changeable">More text that will be changed</p>
<a href="#" onclick="function to change the style of the p class changeable">Click</a>
[/HTML]

(credit - script found at http://lists.evolt.org/archive/Week-of-Mon-20021021/125486.html)
Oct 24 '07 #2
iam_clint
1,208 Expert 1GB
to make a workaround for all browsers you would want to do something like
Expand|Select|Wrap|Line Numbers
  1. elements = "A, P, FONT, DIV, SPAN";
  2. elements = elements.split(",");
  3. for (i=0; i<elements.length; i++) {
  4.  curElements =  document.getElementsByTagName(elements[i]);
  5.  for (c=0; c<curElements.length; c++) {
  6.  document.getElementsByTagName(elements[i])[c].className='blah';
  7.  }
  8. }
  9.  
^^^ above code may not be fully functional because i just typed it up without testing it but you should get the idea

This method would use2 different classes where u just change the class instead of the styles in the class
Oct 24 '07 #3
mrhoo
428 256MB
IE uses the methods addRule and removeRule instead of the standard insert and delete- and the arguments you need to supply are also different from the standard.
Oct 24 '07 #4
TAL651
20
Thank you for your replies. After some more digging around, I came across a code that would do what I needed (i.e. I emailed the person who posted the original script I found halfway worked). Here is the script that works in Firefox, Safari for Windows, IE6 and IE7:

Expand|Select|Wrap|Line Numbers
  1. function changeStyles()
  2. {
  3.    var ie = false;
  4.    var ss = document.styleSheets;
  5.    if ( ss[0].rules )  {  ie = true; ss[0].cssRules = ss[0].rules;  }
  6.    for (i=0;i<ss[0].cssRules.length;i++)
  7.    {
  8.      if (  ss[0].cssRules[i].selectorText.indexOf(".changeable") > -1 )
  9.      {
  10.        if ( ie )
  11.        {
  12.          ss[0].removeRule(i); // this is a little iffy -- it assumes
  13.                               // that the "rules" collection that I
  14.                               // copied into "cssRules" was stored,
  15.                               // and is being returned, in the same
  16.                               // order.  Seems to work, but... :-)
  17.          ss[0].addRule(".changeable","color: red;", i);
  18.        }
  19.        else
  20.        {
  21.          ss[0].deleteRule(i);
  22.          ss[0].insertRule(".changeable { color: red; }", i);
  23.        }
  24.      }
  25.    }
  26. }
  27.  
(Credit - Hassan Schroeder, webtuitive.com)
Oct 26 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

12
by: Christopher J. Bottaro | last post by:
If I have the following class: class MyClass: def __init__(self): m_dict = {} m_dict = 1 m_dict = 2 m_dict = 3 Is there anyway to generate automatic accessors to the elements of the dict?
3
by: CJM | last post by:
I'm adding some extra features to an intranet-based ASP application... As part of the process, I thought I would give the html a mid-life upgrade, ie. remove much of the tag-soup, replace with...
9
by: bissatch | last post by:
Hi, Is it possible to change the class style of an HTML element using DHTML? For example... <td class="my_class">Text</td> I have used DHTML to change style elements such as backgroundColor...
1
by: asilverpeach | last post by:
Hey Guys! Found some great scripts here on this topic but have to make to changes to the code that I can't seem to figure out. First, In the following code clicking on the headers shows the...
4
by: SteveKlett | last post by:
I have a subset of form items that I need to perform different operations on (enable/disable, clear values, change style, etc) rather than hard code the IDs or names I would like to recursively...
18
by: Diogenes | last post by:
Hi All; I, like others, have been frustrated with designing forms that look and flow the same in both IE and Firefox. They simply did not scale the same. I have discovered, to my chagrin,...
15
by: worked | last post by:
I have a script that hides / shows form elements, and wrapped in a tab script (tab navigation). When the code is duplicated (per tab content), the hide / show function works for the first tab but...
13
by: windsorben | last post by:
I have some javascript that checks whether or not an answer is correct. It was working fine when the question was asked with text but now that I'm asking the question with audio, the javascript no...
10
by: Janis Papanagnou | last post by:
The task seems simple but I am not sure whether this is possible or even the right way to look at it... I have <td class="X"elements where "X" is a placeholder for class names "A", "B", etc. ...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.