473,670 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7920


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.styleS heets[0].cssRules[0].cssPropertyNam e
with Mozilla (and other W3C DOM Level 2 compliant browsers) and
document.styleS heets[0].rules[0].cssPropertyNam e
with IE. A simple example would be

<html>
<head>
<title>Scriptin g CSS stylesheet rules</title>
<style type="text/css">
li { }
</style>
<script type="text/javascript">
function setBorder (borderWidth, borderStyle, borderColor) {
var styleSheet, cssRule;
if (document.style Sheets) {
styleSheet = document.styleS heets[0];
if (styleSheet) {
if (styleSheet.css Rules) {
cssRule = styleSheet.cssR ules[0];
}
else if (styleSheet.rul es) {
cssRule = styleSheet.rule s[0];
}
if (cssRule) {
cssRule.style.b orderWidth = borderWidth;
cssRule.style.b orderStyle = borderStyle;
cssRule.style.b orderColor = borderColor;
}
}
}
}
</script>
</head>
<body>
<p>
<input type="button" value="border: 2px solid green"
onclick="setBor der('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.style Sheets) {
if (document.style Sheets.length === 0) {
var styleElement;
if (document.creat eElement && (styleElement =
document.create Element('style' ))) {
styleElement.ty pe = 'text/css';
document.getEle mentsByTagName( 'head')[0].appendChild(st yleElement);
styleSheet = styleElement.sh eet;
}
}
if (document.style Sheets.length > 0) {
styleSheet = document.styleS heets[document.styleS heets.length - 1];
if (styleSheet.ins ertRule) {
styleSheet.inse rtRule(
selectorText + ' { ' + declarations + ' }',
styleSheet.cssR ules.length
);
}
else if (styleSheet.add Rule) {
styleSheet.addR ule(selectorTex t, declarations);
}
}
}
}
</script>
</head>
<body>
<p>
<input type="button" value="li {border: 2px solid green; }"
onclick="addCSS Rule('li', 'border: 2px solid green;');">
<input type="button" value="input { font-size: 110%; }"
onclick="addCSS Rule('input', 'font-size: 110%;');">
<ul>
<li>Kibo</li>
<li>Xibo</li>
</ul>
</body>
</html>
Opera 7 has no document.styleS heets support but if you add a style
element then the style rules are applied:

var styleElement = document.create Element('style' );
styleElement.ty pe = 'text/css';
styleElement.ap pendChild(docum ent.createTextN ode('body {
background-color: lightblue; }'));
document.getEle mentsByTagName( 'head')[0].appendChild(st yleElement);

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
3901
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 in the os module got objectified properly (or at least with some semblance of OO propriety!) In the issues section:
3
3028
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 in a form text box, and it returns the manipulated text. Ideally, I'd like it to return teh text in teh same page, without reloading anything from the server. Problem is, I have no idea how to capture anything entered into a form with...
15
9055
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 the purpose of learning to work with C++. It therefore makes some sense for me to give the situation the amount of consideration presented below. To be quite honest, I'm amazed at the amount there is to say about such a seemingly simple...
1
2070
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 normal WinForm GUI thread. How would I go about doing this? I have this code for cross-thread WinForm manipulation, but only from within the WinForm class:
4
2844
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. '**************************************************************************** ' Issues '****************************************************************************
5
1721
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 CSV file while removing duplicate records. The main problem is that the script currently takes about 20 seconds to execute, and were it to take much longer it would time out. Below is the script itself, and then some example lines from the log...
0
2500
by: metaperl | last post by:
A Comparison of Python Class Objects and Init Files for Program Configuration ============================================================================= Terrence Brannon bauhaus@metaperl.com http://www.livingcosmos.org/Members/sundevil/python/articles/a-comparison-of-python-class-objects-and-init-files-for-program-configuration/view
1
942
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 command such as: workbook = xlrd.open_workbook('C:/trial.txt'), its giving me errors saying "expected BOF record". I was wondering how to work around this. Do I have to do some format conversion to accomplish this or am I missing something Thanks...
0
8469
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
8386
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
8661
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7419
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6213
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
4211
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
4391
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2800
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
2
1794
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.