mlse
12-22-2005, 03:38 PM
Hi.
I have some Javascript knocking around that I wrote a while ago.
In essence, what it does is to change certain style attributes of elements referenced by ID when a button is pressed.
For example:
Javascript:
function doChange(id)
{
document.getElementById(id).style.color="#FF0000";
document.getElementById(id).style.background-color="#00FF00";
document.getElementById(id).style.border-color="#0000FF";
/* and other stuff */
}
HTML:
<div id="thediv" class="theclass">
<p>Some Text Here</p>
<input type="button" value="click me" onclick="doChange('thediv');">
</div>
CSS:
DIV.theclass {
border:2px solid;
margin:100px 20px;
padding:70px;
}
Ok, this works fine, but what I would like to do is to be able to remove style information from the Javascript in order to make the script more generic.
Last example modified to show what I want to do:
Javascript:
function changeTheClass(obj, oldclass, newclass)
{
/* *** The magic class-changing code goes here! *** */
}
function doChange(id, oldclass, newclass)
{
changeTheClass(document.getElementById(id), oldclass, newclass);
/* and other stuff */
}
HTML:
<div id="thediv" class="theclass beforehand">
<p>Some Text Here</p>
<input type="button" value="click me" onclick="doChange('thediv', 'beforehand', 'afterwards');">
</div>
CSS:
DIV.theclass {
border:2px solid;
margin:100px 20px;
padding:70px;
}
DIV.beforehand{
color:#FFFFFF;
background-color:#999999;
border-color:#000000;
}
DIV.afterwards{
color:#FF0000;
background-color:#00FF00;
border-color:#0000FF;
}
The burning question is ... what is the magic code that goes in the changeTheClass function?? It must be able to change the class from "beforehand" to "afterwards" while preserving other class information (i.e. it must be able to change the class of the div from "theclass beforehand" to "theclass afterwards" - hence why I have included both "oldclass" and "newclass" in the function prototype - the function will need to know which part of the class specification to change!).
I know what I want to do, I just don't know the syntax! Any help on this much appreciated!
TIA,
Mike.
I have some Javascript knocking around that I wrote a while ago.
In essence, what it does is to change certain style attributes of elements referenced by ID when a button is pressed.
For example:
Javascript:
function doChange(id)
{
document.getElementById(id).style.color="#FF0000";
document.getElementById(id).style.background-color="#00FF00";
document.getElementById(id).style.border-color="#0000FF";
/* and other stuff */
}
HTML:
<div id="thediv" class="theclass">
<p>Some Text Here</p>
<input type="button" value="click me" onclick="doChange('thediv');">
</div>
CSS:
DIV.theclass {
border:2px solid;
margin:100px 20px;
padding:70px;
}
Ok, this works fine, but what I would like to do is to be able to remove style information from the Javascript in order to make the script more generic.
Last example modified to show what I want to do:
Javascript:
function changeTheClass(obj, oldclass, newclass)
{
/* *** The magic class-changing code goes here! *** */
}
function doChange(id, oldclass, newclass)
{
changeTheClass(document.getElementById(id), oldclass, newclass);
/* and other stuff */
}
HTML:
<div id="thediv" class="theclass beforehand">
<p>Some Text Here</p>
<input type="button" value="click me" onclick="doChange('thediv', 'beforehand', 'afterwards');">
</div>
CSS:
DIV.theclass {
border:2px solid;
margin:100px 20px;
padding:70px;
}
DIV.beforehand{
color:#FFFFFF;
background-color:#999999;
border-color:#000000;
}
DIV.afterwards{
color:#FF0000;
background-color:#00FF00;
border-color:#0000FF;
}
The burning question is ... what is the magic code that goes in the changeTheClass function?? It must be able to change the class from "beforehand" to "afterwards" while preserving other class information (i.e. it must be able to change the class of the div from "theclass beforehand" to "theclass afterwards" - hence why I have included both "oldclass" and "newclass" in the function prototype - the function will need to know which part of the class specification to change!).
I know what I want to do, I just don't know the syntax! Any help on this much appreciated!
TIA,
Mike.