Software, your way.
How To Get Good Custom Software
(Download)
(PDF)
burger menu icon
WillMaster

WillMaster > LibraryWebsite Owner Tools

FREE! Coding tips, tricks, and treasures.

Possibilities weekly ezine

Get the weekly email website developers read:

 

Your email address

name@example.com
YES! Send Possibilities every week!

Power User of Powerhouse CSS Style Changer

The Powerhouse CSS Style Changer function changes the CSS style of individual HTML elements on the current web page to the styles you specify.

The JavaScript PowerhouseStyleChange() does an excellent job, elegantly. Whatever CSS an HTML tag (such as div and p) can have can be added or changed with a call to that function.

But it can change only one HTML element at a time.

This article has a StyleChangeForPowerhouse() function that accepts CSS changes for numerous HTML elements.

StyleChangeForPowerhouse() works by calling PowerhouseStyleChange() for each HTML element with CSS to change.

An Illustration

Here is an illustration. Click this link to change the border, padding, and font family of the first div and, of the second div, switch text and background colors. Then click this link to reverse the changes.

Testing
Testing

The code to reproduce the illustration on your test page is further below. But first, the JavaScript functions.

The JavaScript Functions

There are two JavaScript functions, StyleChangeForPowerhouse() and PowerhouseStyleChange().

The first function, StyleChangeForPowerhouse(), is new in this article. It needs no customization.

That second function, PowerhouseStyleChange(), is copied straight out of the Powerhouse CSS Style Changer article. It also needs no customization.

Here is the JavaScript with both functions. It can be placed anywhere in your web page source code that JavaScript can run. Near the bottom of the page, above the cancel </body> tag is generally a good spot.

<script type="text/javascript">

function StyleChangeForPowerhouse()
{
   for( var i=0; i< arguments.length; i++ ) 
   {
        var ta = arguments[i].split("=",2);
        PowerhouseStyleChange(ta[0],ta[1]);
   }
}

function PowerhouseStyleChange(idvalue,style)
{
   // Version 1.0, March 14, 2017.
   // Version 1.1, November 10, 2017 (removed .replace(...) from last code line)
   // Will Bontrager Software LLC - https://www.willmaster.com/
   var id = document.getElementById(idvalue);
   var ta = style.split(/\s*;\s*/);
   for( var i=0; i<ta.length; i++ )
   {
      var tta = ta[i].split(':',2);
      var ttaa = tta[0].split('-');
      if( ttaa.length > 1 )
      {
         for( var ii=1; ii<ttaa.length; ii++ )
         {
             ttaa[ii] = ttaa[ii].charAt(0).toUpperCase() + ttaa[ii].slice(1);
         }
      }
      tta[0] = ttaa.join('');
      id.style[tta[0]] = tta[1];
   }
}

</script>

Using the JavaScript

A link or other triggers (such as onmouseover) can call the StyleChangeForPowerhouse() function.

The StyleChangeForPowerhouse() function can accept new CSS declarations for any number of HTML elements.

The style for an HTML element is specified like this:

ID=style

Replace ID with the id value of the HTML element. And replace style with the CSS style to update the HTML element with.

Example:

element-one=border:9px solid #ccc; font-family:courier;

When specifying a style, use the format you would use if you were typing inline CSS declarations.

To call the StyleChangeForPowerhouse() function, specify ID=style between quotes. For multiple elements, separate each quoted ID=style set with a comma.

Here is an example with ID=style for two HTML elements.

StyleChangeForPowerhouse("ID=style","ID=style")

And here is an example with actual CSS for two HTML elements.

StyleChangeForPowerhouse("element-one=border:9px solid #ccc; font-family:courier;","element-two=color:gold; background-color:purple;")

Reproducing the Illustration

To reproduce the illustration, put the following code on a web page along with the JavaScript functions StyleChangeForPowerhouse() and PowerhouseStyleChange() from the code box further above. Notes follow.

<div id="element-one" 
    style="float:left; 
           width:200px; 
           height:50px; 
           font-family:sans-serif;
           font-size:14pt;
           border:1px solid #000;
           padding-top:13px;
           margin-right:3px;
           text-align:center;
           box-sizing:border-box;">
Testing
</div>
<div id="element-two" 
    style="float:left; 
           width:200px; 
           height:50px; 
           color:#666666;
           background-color:#cccc99;
           font-size:14pt;
           font-weight:bold;
           border:1px solid #ccc;
           padding:13px;
           text-align:center;
           box-sizing:border-box;">
Testing
</div>
<div style="clear:left;"></div>


<p>
<a href="javascript:ChangeThem()">Click this link</a> to change the border and font family of the first div and switch text and background colors of the second div. 
Then <a href="javascript:ReverseChanges()">click this link</a> to reverse the changes.
</p>


<script>
function ChangeThem()
{
    StyleChangeForPowerhouse("element-one=border:9px solid #ccc; padding-top:5px; font-family:courier;","element-two=color:#cccc99; background-color:#666666;");
}

function ReverseChanges()
{
    StyleChangeForPowerhouse("element-one=border:1px solid #000; padding-top:14px; font-family:sans-serif;","element-two=color:#666666; background-color:#cccc99;");
}
</script>

Notes:

The id value for each div is colored blue to make it easier to correspond with the JavaScript below the div HTML.

The CSS declarations that the JavaScript changes are colored red in the div HTML. (The JavaScript could be used to introduce new CSS declarations. It's not restricted to only changing what's present.)

Below the div HTML, you'll see example links. And below that, the JavaScript to make the changes. In the JavaScript, the calls to StyleChangeForPowerhouse() have their parameters color coded like previous examples.

You now have a power user technique for Powerhouse CSS Style Changer. Change the styles of two or many HTML elements with one JavaScript function call.

(This article first appeared in Possibilities newsletter.)

Will Bontrager

Was this article helpful to you?
(anonymous form)

Support This Website

Some of our support is from people like you who see the value of all that's offered for FREE at this website.

"Yes, let me contribute."

Amount (USD):

Tap to Choose
Contribution
Method

All information in WillMaster Library articles is presented AS-IS.

We only suggest and recommend what we believe is of value. As remuneration for the time and research involved to provide quality links, we generally use affiliate links when we can. Whenever we link to something not our own, you should assume they are affiliate links or that we benefit in some way.

How Can We Help You? balloons
How Can We Help You?
bullet Custom Programming
bullet Ready-Made Software
bullet Technical Support
bullet Possibilities Newsletter
bullet Website "How-To" Info
bullet Useful Information List

© 1998-2001 William and Mari Bontrager
© 2001-2011 Bontrager Connection, LLC
© 2011-2024 Will Bontrager Software LLC