Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8363603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:13:08+00:00 2026-06-09T12:13:08+00:00

I am trying to set it so the borders change color on different elements

  • 0

I am trying to set it so the borders change color on different elements on my site based on which page the user is currently on. I am using PHP to determine the page, and then setting the variable $color (within the JS function) based on the PHP determination

For some reason, this will not work at all. I’ve even tried putting the function in a onmouseover to see if it would work, and it would not.

I must have an error somewhere within this JS function. I’ve got other ones that I’ve put together, and they are working fine. I am very new to JS, so I may not pick up on the obvious errors yet.

Here is the JS function I am trying to load.

function colorchange($color) {
    var $header = document.getElementsByClassName('header');
    $header.style.borderColor = $color;

    var $contentblock = document.getElementsByClassName('contentblock');
    $contentblock.style.borderColor = $color;

    var $rightfloat = document.getElementsByClassName('rightfloat');
    $rightfloat.style.borderColor = $color;

    var $footer = document.getElementsByClassName('footer');
    $footer.style.borderColor = $color;

    var $mainimg = document.getElementsByClassName('mainimg');
    $mainimg.style.borderColor = $color;

    var $menucontainer = document.getElementsByClassName('menucontainer');
    $menucontainer.style.borderColor = $color;
}

I am calling it here:

<body onload="<?php echo $changecolor; ?>">

And this is the PHP snippet to determine the color.

if (isset($pageid)) {
    if ($pageid == "ministries") {
        $changecolor = "colorchange('#a52926');";
    }
    if ($pageid == "events") {
        $changecolor = "colorchange('#a54a4c');";
    }
    if ($pageid == "prayer") {
        $changecolor = "colorchange('#459979');";
    }
    if ($pageid == "about") {
        $changecolor = "colorchange('#a55029');";
    }
    if ($pageid == "contact") {
        $changecolor = "colorchange('#469e47');";
    }
} else {
    $changecolor = "colorchange('#26996d');";
}

Thanks for your time and help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T12:13:10+00:00Added an answer on June 9, 2026 at 12:13 pm

    document.getElementsByClassName returns multiple elements, not just one. Because of this, you cannot do .style on it. You need to loop through the returned elements and set .style on each one.

    var $header = document.getElementsByClassName('header');
    for(var i = 0, len = $header.length; i < len; i++){
        $header[i].style.borderColor = $color;
    }
    

    EDIT: I suggest giving all the elements you want to change the color of the same class (for example: changeColor). That way you don’t need all those different getElementsByClassName statements.

    function colorchange(color) {
        var colorChange = document.getElementsByClassName('colorChange');
            for(var i = 0, len = colorChange.length; i < len; i++){
                colorChange[i].style.borderColor = color;
            }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to change the color of links inside <li> elements by setting
I am using fancybox for an image gallery. I am currently trying to set
Trying to set up a self signed certificate, for our intranet's web services site.
im trying to set the height of two divs on my page, because my
Working in WPF, writing a custom user control. I am trying to change the
I'm trying to create arrows using borders but FF10 on win7 doesn't want to
I'm trying to set the width's for the border using jQuery, so it'll be
I'm trying to set up a simple horizontal tab structure for a page I'm
I'm trying to set transparent icons in a QAction, which is then added to
I'm trying to create a div which where I can set the opacity for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.