473,395 Members | 1,527 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.

Doing CSS via JavaScript

Hi,

I have a single line of CSS that I want to do via JavaScript :-

p { margin: 0px; }

How do I go about this ?

Many thanks in advance,

Aaron


Apr 8 '06 #1
18 3206
> I have a single line of CSS that I want to do via JavaScript

What does the "do" mean? Where does this need to be inserted? Can it be
inserted into the head element of your html doc?

Peter

Apr 8 '06 #2
>> I have a single line of CSS that I want to do via JavaScript

What does the "do" mean?
Where does this need to be inserted? Can it be
inserted into the head element of your html doc?


I have a document in an IFrame and want to modify its CSS setting to make
paragraphs have zero margins.

Aaron
Apr 8 '06 #3
Aaron Gray wrote:
Hi,

I have a single line of CSS that I want to do via JavaScript :-

p { margin: 0px; }

How do I go about this ?

It's easy to change the style on a single element in JavaScript, but in
order to do what you want, you will have to find all the p elements and
apply the style to each element returned.

Something like (untested)

var paras = document.getElementsByTagName('p');

for( var i = paras.length-1; i >= ); --i )
{
paras[i].style.margin = '0px';
}

--
Ian Collins.
Apr 8 '06 #4
> Aaron Gray wrote:
Hi,

I have a single line of CSS that I want to do via JavaScript :-

p { margin: 0px; }

How do I go about this ?

It's easy to change the style on a single element in JavaScript, but in
order to do what you want, you will have to find all the p elements and
apply the style to each element returned.

Something like (untested)

var paras = document.getElementsByTagName('p');

for( var i = paras.length-1; i >= ); --i )
{
paras[i].style.margin = '0px';
}


No I need to alter the CSS for the document not the actual document itself.

Aaron
Apr 8 '06 #5
Aaron Gray wrote:
Aaron Gray wrote:
Hi,

I have a single line of CSS that I want to do via JavaScript :-

p { margin: 0px; }

How do I go about this ?


It's easy to change the style on a single element in JavaScript, but in
order to do what you want, you will have to find all the p elements and
apply the style to each element returned.

Something like (untested)

var paras = document.getElementsByTagName('p');

for( var i = paras.length-1; i >= ); --i )
{
paras[i].style.margin = '0px';
}

No I need to alter the CSS for the document not the actual document itself.

Why and to what end?
--
Ian Collins.
Apr 8 '06 #6
"Ian Collins" <ia******@hotmail.com> wrote in message
news:49************@individual.net...
Aaron Gray wrote:
No I need to alter the CSS for the document not the actual document
itself.

Why and to what end?


Ah. Now that would be telling, just a project I am working on :)

I think I have a handle on doing this now, but am really working in the
dark.

Aaron
Apr 8 '06 #7
Aaron Gray wrote:
I have a single line of CSS that I want to do via JavaScript


What does the "do" mean?
Where does this need to be inserted? Can it be
inserted into the head element of your html doc?

I have a document in an IFrame and want to modify its CSS setting to make
paragraphs have zero margins.

I'm sure you'll find you have to do it the way I suggested, styles are
applied while the markup is rendered, before onload for the page gets
called. So once the page is displayed, you have to change the style on
each element directly.

--
Ian Collins.
Apr 8 '06 #8

"Ian Collins" <ia******@hotmail.com> wrote in message
news:49************@individual.net...
I'm sure you'll find you have to do it the way I suggested, styles are
applied while the markup is rendered, before onload for the page gets
called. So once the page is displayed, you have to change the style on
each element directly.


Try this then :-

function SetClassStyle( clazz, style, value)
{
if (document.styleSheets)
{
for( var styleSheetNo = 0; styleSheetNo < document.styleSheets.length;
styleSheetNo++)
{
var styleSheet = document.styleSheets[styleSheetNo];
var rules = styleSheet.rules | styleSheet.cssRules;

if (!rules) return; // err ???

for ( var rule = 0; rule < rules.length; rule++)
if ( rules[rule].selectorText.toLowerCase() == clazz)
rules[rule].style[style] = value;
}
}
else // No stylesheet
{
var all = getElementByTagName("*");

for ( var element = 0; element < all.length; element++)
if ( all[element].className == clazz)
all[element].style[style] = value;
}
}

Only if there is no stylesheet do you have to do it by tags !:)

Aaron
Apr 8 '06 #9
Aaron Gray wrote:
"Ian Collins" <ia******@hotmail.com> wrote in message
news:49************@individual.net...
I'm sure you'll find you have to do it the way I suggested, styles are
applied while the markup is rendered, before onload for the page gets
called. So once the page is displayed, you have to change the style on
each element directly.

Try this then :-

function SetClassStyle( clazz, style, value)
{
if (document.styleSheets)
{
for( var styleSheetNo = 0; styleSheetNo < document.styleSheets.length;
styleSheetNo++)
{
var styleSheet = document.styleSheets[styleSheetNo];
var rules = styleSheet.rules | styleSheet.cssRules;

if (!rules) return; // err ???

for ( var rule = 0; rule < rules.length; rule++)
if ( rules[rule].selectorText.toLowerCase() == clazz)
rules[rule].style[style] = value;
}
}
else // No stylesheet
{
var all = getElementByTagName("*");

for ( var element = 0; element < all.length; element++)
if ( all[element].className == clazz)
all[element].style[style] = value;
}
}

Only if there is no stylesheet do you have to do it by tags !:)

OK, if this works, I've learnt my lesson for the day!

--
Ian Collins.
Apr 8 '06 #10
Aaron Gray wrote:
"Ian Collins" <ia******@hotmail.com> wrote in message
news:49************@individual.net...
I'm sure you'll find you have to do it the way I suggested, styles are
applied while the markup is rendered, before onload for the page gets
called. So once the page is displayed, you have to change the style on
each element directly.

Try this then :-

Thinking about is a bit more, why do this when you can simply use a
style tag?

Also is this is to run before styles are applied, there won't be any
elements in the DOM beyond the current script, so the else part is void.
function SetClassStyle( clazz, style, value)
{
if (document.styleSheets)
{
for( var styleSheetNo = 0; styleSheetNo < document.styleSheets.length;
styleSheetNo++)
{
var styleSheet = document.styleSheets[styleSheetNo];
var rules = styleSheet.rules | styleSheet.cssRules;

if (!rules) return; // err ???

for ( var rule = 0; rule < rules.length; rule++)
if ( rules[rule].selectorText.toLowerCase() == clazz)
rules[rule].style[style] = value;
}
}
else // No stylesheet
{
var all = getElementByTagName("*");

for ( var element = 0; element < all.length; element++)
if ( all[element].className == clazz)
all[element].style[style] = value;
}
}

Only if there is no stylesheet do you have to do it by tags !:)

Aaron

--
Ian Collins.
Apr 8 '06 #11

"Ian Collins" <ia******@hotmail.com> wrote in message
news:49************@individual.net...
Thinking about is a bit more, why do this when you can simply use a
style tag?
Belive me my app needs it to do a work around on a work around. Its pritty
hairy stuff at the edges of browser behaviour.
Also is this is to run before styles are applied, there won't be any
elements in the DOM beyond the current script, so the else part is void.


Right.

It will not create a new style rule at the moment thats what I am stuck on
now.

Anyone know how to add a new rule to the CSS using this javascript approach
?

Aaron


Apr 8 '06 #12
Aaron Gray wrote:
"Ian Collins" <ia******@hotmail.com> wrote in message
news:49************@individual.net...
Thinking about is a bit more, why do this when you can simply use a
style tag?

Belive me my app needs it to do a work around on a work around. Its pritty
hairy stuff at the edges of browser behaviour.

Also is this is to run before styles are applied, there won't be any
elements in the DOM beyond the current script, so the else part is void.

Right.

It will not create a new style rule at the moment thats what I am stuck on
now.

Anyone know how to add a new rule to the CSS using this javascript approach
?

Do you control what's in the iframe you mentioned? That's what's
confusing me, if you are displaying your own page, you can control when
scripts are run, if not, you have to fix it after the page has loaded.

--
Ian Collins.
Apr 9 '06 #13
"Ian Collins" <ia******@hotmail.com> wrote in message
news:49************@individual.net...
Do you control what's in the iframe you mentioned? That's what's
confusing me, if you are displaying your own page, you can control when
scripts are run, if not, you have to fix it after the page has loaded.


Yes, the IFrame is user edittable using HTML DesignMode.

There are several bugs in the MS implementation of design mode that I am
doing work arounds for.

Aaron
Apr 9 '06 #14
Aaron Gray wrote:
"Ian Collins" <ia******@hotmail.com> wrote in message
news:49************@individual.net...
Do you control what's in the iframe you mentioned? That's what's
confusing me, if you are displaying your own page, you can control when
scripts are run, if not, you have to fix it after the page has loaded.

Yes, the IFrame is user edittable using HTML DesignMode.

There are several bugs in the MS implementation of design mode that I am
doing work arounds for.

Ah, 'bugs' and 'MS' in the same sentence....

Then you do have to change the page after it has loaded. So you will
have to go through each element.

--
Ian Collins.
Apr 9 '06 #15

"Ian Collins" <ia******@hotmail.com> wrote in message
news:49************@individual.net...
Aaron Gray wrote:
"Ian Collins" <ia******@hotmail.com> wrote in message
news:49************@individual.net...
Do you control what's in the iframe you mentioned? That's what's
confusing me, if you are displaying your own page, you can control when
scripts are run, if not, you have to fix it after the page has loaded.

Yes, the IFrame is user edittable using HTML DesignMode.

There are several bugs in the MS implementation of design mode that I am
doing work arounds for.

Ah, 'bugs' and 'MS' in the same sentence....


Yep.
Then you do have to change the page after it has loaded. So you will
have to go through each element.


No, hopefully if I can get the darn thing to work now I will be onto the
next bug...no reloads...no iterations...

Unfortunately I cannot get the code I showed earlier to work properly now,
its from Google and definately used to work fine !?

Aaron
Apr 9 '06 #16
"Aaron Gray" <an********@gmail.com> wrote in message
news:49************@individual.net...
I have a single line of CSS that I want to do via JavaScript :-

p { margin: 0px; }


document.styleSheets[0].addRule( "P", "margin:0px");

It came down to a one liner :)

Aaron

Apr 9 '06 #17
Aaron Gray wrote:
function SetClassStyle( clazz, style, value)
{
if (document.styleSheets)
{
for( var styleSheetNo = 0; styleSheetNo < document.styleSheets.length;
styleSheetNo++)
for (var styleSheetNo = document.styleSheets.length; styleSheetNo--;)

See below.
{
var styleSheet = document.styleSheets[styleSheetNo];
var rules = styleSheet.rules | styleSheet.cssRules;
You definitely want

var rules = styleSheet.rules || styleSheet.cssRules;

instead. `|' is the binary OR (converting its operands to number and
evaluating to a number), you want the logical one (converting the operands
to boolean and evaluating the second operand only if the first one yields
`false'.
if (!rules) return; // err ???

for ( var rule = 0; rule < rules.length; rule++)
Otherwise, this loop will never execute as the number assigned has no
`length' property. It should also be

for (var rule = rules.length; rule--;)

which is more efficient.
if ( rules[rule].selectorText.toLowerCase() == clazz)
This will never be true, as class selectors start with a dot (".").
Unless you expect the caller to provide the dot prefix, of course.

It will also not apply for class selectors as combined simple selector.
rules[rule].style[style] = value;
}
}
else // No stylesheet
{
var all = getElementByTagName("*");

for ( var element = 0; element < all.length; element++)
for (var element = all.length; element--;)

You will observe that, in fact, you need only two iterator variables here.
if ( all[element].className == clazz)
Same problem as above.
[...]

PointedEars
Apr 9 '06 #18
"Thomas 'PointedEars' Lahn" <Po*********@web.de> wrote in message
news:58****************@PointedEars.de...
Aaron Gray wrote:
function SetClassStyle( clazz, style, value)
{
if (document.styleSheets)
{
for( var styleSheetNo = 0; styleSheetNo < document.styleSheets.length;
styleSheetNo++)


for (var styleSheetNo = document.styleSheets.length; styleSheetNo--;)

See below.
{
var styleSheet = document.styleSheets[styleSheetNo];
var rules = styleSheet.rules | styleSheet.cssRules;


You definitely want

var rules = styleSheet.rules || styleSheet.cssRules;

instead. `|' is the binary OR (converting its operands to number and
evaluating to a number), you want the logical one (converting the operands
to boolean and evaluating the second operand only if the first one yields
`false'.
if (!rules) return; // err ???

for ( var rule = 0; rule < rules.length; rule++)


Otherwise, this loop will never execute as the number assigned has no
`length' property. It should also be

for (var rule = rules.length; rule--;)

which is more efficient.
if ( rules[rule].selectorText.toLowerCase() == clazz)


This will never be true, as class selectors start with a dot (".").
Unless you expect the caller to provide the dot prefix, of course.

It will also not apply for class selectors as combined simple selector.
rules[rule].style[style] = value;
}
}
else // No stylesheet
{
var all = getElementByTagName("*");

for ( var element = 0; element < all.length; element++)


for (var element = all.length; element--;)

You will observe that, in fact, you need only two iterator variables here.
if ( all[element].className == clazz)


Same problem as above.
[...]

PointedEars


That was reworked Googles code; although they had a . prefix added to clazz.

Thanks, I will do a rewrite :)

Aaron
Apr 9 '06 #19

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

Similar topics

6
by: Paul | last post by:
I use ASP and I need to know how to attaches an Iframe Id with its page so I can load menu options ( eg. 1.htm, 2.htm, 3.htm, ect...) into the Iframe from any given page on my website. The problem...
6
by: Paul | last post by:
HI! I get an error with this code. <SCRIPT language="JavaScript"> If (ifp==""){ ifp="default.htm"} //--></SCRIPT> Basicly I want my iframe to have a default page if the user enters in...
8
by: John Dalberg | last post by:
Hello For a single signon solution, I am putting a login form for a remote server on my own site. When the form gets posted, the asp.net creates a httpwebrequest object, does the post, gets the...
4
by: John Boy | last post by:
Hi, Can anyone help. This is really doing my nut in. 3 years ASP exp. and now doing .DOT which is a step in the wrong direction. Basically I am left with the code of a guy who has left. When I...
5
by: Joey | last post by:
I am attempting to hook the onChange event for a dropdown list with javascript so that I can do some stuff and then initiate a postback with my code. I have not yet learned about how to do...
2
by: Rob Roberts | last post by:
Is there any way to prevent a ButtonField in a GridView from doing a postback when clicked? I want to use my own onclick handler to call window.open('SomePage.aspx') to display a page in a new...
2
by: UJ | last post by:
Is there a way with a asp:checkbox to run a JavaScript to display/hide text/input on the screen without doing a postback? I also need to be able to access the stuff at the server so I need to...
28
by: fred.haab | last post by:
Not having server side scripting, I've been doing this for "last modified" tags on my pages: <div class="modified"> <script type="Text/JavaScript"> <!-- document.write("This page was last...
16
by: SirG | last post by:
I'm looking for an explanation of why one piece of code works and another does not. I have to warn you that this is the first piece of Javascript I've ever written, so if there is a better way or a...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.