Save style settings

S

seans

Hi,

I need to change the font, font size, and color of a button on a form.
I need to save the current style settings and restore them later. Is
there an easy way of doing that?

Sorry if there is a simple solution to this; I am still learning
Javascript.

Thanks again.

sean
 
S

Stephen Chalmers

seans said:
Hi,

I need to change the font, font size, and color of a button on a form.
I need to save the current style settings and restore them later. --

What do you mean by 'later' ?

a) Later during the same document load.
b) After a reload during the same browser session.
c) On a subsequent browser session.
d) Other

How are you changing the style values?

a) By swapping to another predefined class.
b) By addressing individual properties.
c) Other
 
S

seans

Hi Stephen thanks for your reply. Sorry about the ambiguity. I am doing
a search for strings in the document. When somebody selects a string
from a drop-down list the string is highligted on the page by changing
the style settings. The font is changed and the color changed to red.
When somebody searches for a different string I want the previously
selected string to revert back to it's original style settings. I am
changing the style by addressing the individual style properties.

thanks again.

sean
 
S

Stephen Chalmers

seans said:
Hi Stephen thanks for your reply. Sorry about the ambiguity. I am doing
a search for strings in the document. When somebody selects a string
from a drop-down list the string is highligted on the page by changing
the style settings. The font is changed and the color changed to red.
When somebody searches for a different string I want the previously
selected string to revert back to it's original style settings. I am
changing the style by addressing the individual style properties.

thanks again.

sean
This is guesswork of course, but presumably the strings to be highlighted are enclosed within <span> or <div> tags,
which initially are styled the same as the surrrounding text.
If this is the case, all that's required is to have two pre-defined style classes and to toggle the className property
of the relevant element.
 
S

seans

Stephen said:
This is guesswork of course, but presumably the strings to be highlighted are enclosed within <span> or <div> tags,
which initially are styled the same as the surrrounding text.
If this is the case, all that's required is to have two pre-defined style classes and to toggle the className property
of the relevant element.

Hi Stephen thanks for your reply. Yes that is what I am trying to do.
The strings are encloses in span tags.

Is this the way to create styles programmatically in JavaScript? I
tried doing the following but it produced an error saying that there
were an invalid number of arguments.

document.createStyleSheet();
with (document.styleSheets(document.styleSheets.length-1)) {
addRule(".highlight","color:red;font-Size: 16pt;font-family:arial");

}

thanks.

sean
 
S

Stephen Chalmers

seans said:
Hi Stephen thanks for your reply. Yes that is what I am trying to do.
The strings are encloses in span tags.

Is this the way to create styles programmatically in JavaScript? I
tried doing the following but it produced an error saying that there
were an invalid number of arguments.

document.createStyleSheet();
with (document.styleSheets(document.styleSheets.length-1)) {
addRule(".highlight","color:red;font-Size: 16pt;font-family:arial");

}

document.styleSheets isn't supported by Opera and is unnecessary.
I advised toggling the element's className.

Try this fragile example:

<HTML>
<HEAD>
<style>
..normText{color:black; font-weight:normal}
..redText{color:red; font-weight:bold}
</style>
</HEAD>
<BODY>
<FORM>

<SELECT name='s1' onchange='hiLite( "theText", this.selectedIndex-1 )'>
<option>Please select a word...
<option>Truth
<option>Indefinite
<option>Doubt
</SELECT>

</FORM>

<DIV id='theText' class='normText'>Any <span>truth</span> is better than <span>indefinite</span>
<span>doubt</span>.</DIV>

<SCRIPT type='text/javascript'>

function hiLite(elem, idx)
{
var spans=document.getElementById( elem ).getElementsByTagName('span');

for(var i=0,len=spans.length; i<len; i++)
spans.className = (i==idx)? 'redText' : 'normText';
}

</SCRIPT>
</BODY>
</HTML>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top