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 9079179
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T19:47:01+00:00 2026-06-16T19:47:01+00:00

My problem is as it follows: I have a piece of JavaScript code which

  • 0

My problem is as it follows:

I have a piece of JavaScript code which generates a "new" document using document.write and at first I write the entire heading of the web page. In that head I have a bit of CSS code which looks like this:

<style type='text/css'> 
.black {background-color: black;} 
.white {background-color: #ffffff;} 
td {width:50px; height:50px;} 
</style>

And JavaScript draws a table in the body section. Writing code is basically two for loops which draw a chess board. But that doesn’t even matter. Every td element gets a class black or white by which it gets properly coloured. And every td gets a onclick='changeBg(this)' attribute.

Here is where the problem comes to life. I can not access the background color of the element that gets clicked. Function looks like so:

function changeBg(element)
{
    alert(element.style.backgroundColor);
    element.style.backgroundColor = "red";
}

At first I alert the color of the current element. It always alerts a blank notification. After I change the color to red and click on the element again it alerts red. The td‘s are coloured in the browser and if I inspect them with firebug the have background-color: black | white;

What am I missing and how to fix this? I have realised that if I set td color when creating them using style="color: black | white"; it works but then I don’t know to which class they belong.

  • 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-16T19:47:02+00:00Added an answer on June 16, 2026 at 7:47 pm

    You can get the currently applied style using window.getComputedStyle – Docs

    function changeBg(element) {
        alert(window.getComputedStyle(element).backgroundColor);
        element.style.backgroundColor = "red";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I.e. I have a piece of code on my page as follows, which is
I have a piece of code written using VS 2005 that works fine in
I have a piece of C++ code that deallocates memory as follows for (int
Newbie here! My problem is as follows: I have a dynamically populating ul where
I'm facing a problem that I can summarize as it follows: I have a
My problem is as follows. I have an array of objects in the form
I have a mysql problem, my query looks as follows but not complete SELECT
A minimal code that reproduces the problem is as follows: <div class=cell> <input type=text
When I write a C program, I encountered a problem that is as follows:
I have two tables which have a structure as follows: `content` (id, foreign_id, type,

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.