473,395 Members | 1,368 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,395 software developers and data experts.

Class manipulation from script

I know how to change attributes of a given element using Javascript, but can
you change an attribute for a CSS class with script? Or for a particular
kind of tag? For example, can you write a single script statement that will
apply a given padding to all elements of a given class, or put a border
around all the LIs on a page?

--
Harlan Messinger
Remove the first dot from my e-mail address.
Veuillez ôter le premier point de mon adresse de courriel.

Jul 20 '05 #1
2 7905


Harlan Messinger wrote:
I know how to change attributes of a given element using Javascript, but can
you change an attribute for a CSS class with script? Or for a particular
kind of tag? For example, can you write a single script statement that will
apply a given padding to all elements of a given class, or put a border
around all the LIs on a page?


Mozilla, Netscape 6/7 and IE allow you to manipulate the CSS stylesheets
and their rules e.g. for Mozilla if you have
<style type="text/css">
li { }
</style>
as the only stylesheet in your page then you can script
document.styleSheets[0].cssRules[0].cssPropertyName
with Mozilla (and other W3C DOM Level 2 compliant browsers) and
document.styleSheets[0].rules[0].cssPropertyName
with IE. A simple example would be

<html>
<head>
<title>Scripting CSS stylesheet rules</title>
<style type="text/css">
li { }
</style>
<script type="text/javascript">
function setBorder (borderWidth, borderStyle, borderColor) {
var styleSheet, cssRule;
if (document.styleSheets) {
styleSheet = document.styleSheets[0];
if (styleSheet) {
if (styleSheet.cssRules) {
cssRule = styleSheet.cssRules[0];
}
else if (styleSheet.rules) {
cssRule = styleSheet.rules[0];
}
if (cssRule) {
cssRule.style.borderWidth = borderWidth;
cssRule.style.borderStyle = borderStyle;
cssRule.style.borderColor = borderColor;
}
}
}
}
</script>
</head>
<body>
<p>
<input type="button" value="border: 2px solid green"
onclick="setBorder('2px', 'solid', 'green');">
<ul>
<li>Kibo</li>
<li>Xibo</li>
</ul>
</body>
</html>

If you don't have a stylesheet or no rule the DOM also allows you to add
those (again, IE (with IE specific methods), and Mozilla/Netscape with
W3C DOM methods)):

<html>
<head>
<title>adding CSS rules</title>
<script type="text/javascript">
function addCSSRule (selectorText, declarations) {
var styleSheet;
if (document.styleSheets) {
if (document.styleSheets.length === 0) {
var styleElement;
if (document.createElement && (styleElement =
document.createElement('style'))) {
styleElement.type = 'text/css';
document.getElementsByTagName('head')[0].appendChild(styleElement);
styleSheet = styleElement.sheet;
}
}
if (document.styleSheets.length > 0) {
styleSheet = document.styleSheets[document.styleSheets.length - 1];
if (styleSheet.insertRule) {
styleSheet.insertRule(
selectorText + ' { ' + declarations + ' }',
styleSheet.cssRules.length
);
}
else if (styleSheet.addRule) {
styleSheet.addRule(selectorText, declarations);
}
}
}
}
</script>
</head>
<body>
<p>
<input type="button" value="li {border: 2px solid green; }"
onclick="addCSSRule('li', 'border: 2px solid green;');">
<input type="button" value="input { font-size: 110%; }"
onclick="addCSSRule('input', 'font-size: 110%;');">
<ul>
<li>Kibo</li>
<li>Xibo</li>
</ul>
</body>
</html>
Opera 7 has no document.styleSheets support but if you add a style
element then the style rules are applied:

var styleElement = document.createElement('style');
styleElement.type = 'text/css';
styleElement.appendChild(document.createTextNode(' body {
background-color: lightblue; }'));
document.getElementsByTagName('head')[0].appendChild(styleElement);

Mozilla also supports that but I think IE doesn't allow the appendChild
on the styleElement object.
--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #2
Thanks for the detailed example! After I wrote, I found where it was
addressed under CSS2, and it looked like a chunk to assimilate. You saved me
a lot of time.

Jul 20 '05 #3

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

Similar topics

31
by: John Roth | last post by:
I'm adding a thread for comments on Gerrit Holl's pre-pep, which can be found here: http://tinyurl.com/2578q Frankly, I like the idea. It's about time that all of the file and directory stuff...
3
by: Fabian | last post by:
I have created a javascript to manipulate a text strong given to it. It works in all the situations I put it in. Now, I want to create a form based interface. Essentially, the use types in the text...
15
by: Steven T. Hatton | last post by:
The following may strike many of you as just plain silly, but it represents the kind of delelima I find myself in when trying to make a design decision. This really is a toy project written for...
1
by: i | last post by:
Hi, I'm trying to get a seperate class, initialized by a form class, to manipulate certain objects on the form (ex: add to a listbox). The manipulation will occur via a thread that is not the...
4
by: Ray Dukes | last post by:
What I am looking to do is map the implementation of interface properties and functions to an inherited method of the base class. Please see below. ...
5
by: Cleverbum | last post by:
I'm not really accustomed to string manipulation and so I was wondering if any of you could be any help i speeding up this script intended to change the format of some saved log information into a...
0
by: metaperl | last post by:
A Comparison of Python Class Objects and Init Files for Program Configuration ============================================================================= Terrence Brannon bauhaus@metaperl.com...
1
by: Krishna | last post by:
I have a script that reads an excel file and and do manipulations on it. But, now, I have a text file that needs the same manipulation. I tried the same script, but it will not work, when I use...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
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,...

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.