0

Hi there I'm having a problem. I made a box of blue color in HTML/CSS and want javascript to alert the name of color when the box is clicked. Here is my code.`

var clr = document.getElementById("box").style.backgroundColor;
document.getElementById("box").onclick= function() {
    alert(clr);
}
<!DOCTYPE html>
<html>

   <head>
  <title>
  </title>
  <style>
     #box {
        height: 100px;
        width: 100px;
        background-color: blue;
        margin: 0px;
        display: inline-block;
     }

  </style>
   </head>

   <body>
  <div id="box" class="box">
  </div>
   </body>

</html>

9
  • you want alert particular property or full css details Commented Jun 14, 2016 at 6:40
  • ..... aaaaand...? What is the status of your problem? What happens? Commented Jun 14, 2016 at 6:41
  • 2
    The background-color is set via CSS and not inline styles, so you should use getComputedStyle Commented Jun 14, 2016 at 6:41
  • alert the name of color....i guess this closes the question itself. Commented Jun 14, 2016 at 6:44
  • 1
    With getcomputed my guess is het will get the RGB values, not the actual name ... Commented Jun 14, 2016 at 6:45

5 Answers 5

1

You need to use getComputedStyle(). .style is use to set a new value for target element.

var div     = document.getElementById("box"), // element
    divCSS  = window.getComputedStyle(div), // element CSS 
    bgColor = divCSS.getPropertyValue('background-color'); // property


document.getElementById("box").onclick= function() {
    alert(bgColor);
}
#box{
  height: 100px;
  width: 100px;
  background-color: blue;
  margin: 0px;
  display: inline-block;
}
<div id="box" class="box"></div>

Sign up to request clarification or add additional context in comments.

3 Comments

Okay, i guessed right , you'll still need some JS to convert the RGB values to a named color
@Yoeri, you're right, but that would require a sort of library, or at last creating a array with values for each for all registered color names.
Indeed, best answer ... no can do without additional translation code.
1

Here is the working code

<!DOCTYPE html>
<html>

   <head>
  <title>
  </title>
  <style>
     #box {
        height: 100px;
        width: 100px;
        background-color: blue;
        margin: 0px;
        display: inline-block;
     }

  </style>
   </head>

   <body>
  <div id="box" class="box">
  </div>
   </body>

</html>
<script>
/*  var clr = document.getElementById("box").style.backgroundColor;*/
    document.getElementById("box").onclick= function() {

    var ele = document.getElementById("box");
    var style = window.getComputedStyle(ele);
    var bColor = style.getPropertyValue("background-color");
    alert(bColor);
    }

</script>

1 Comment

Ok. It gives the rgb value. Any way to get the name of colour like blue, actually I'm trying to make a color game in which user will told to click on given color and user will click on the box and programme will check whether it is the same color that was told to him.
-1

This works

var clr = document.getElementById("box").style.backgroundColor;
document.getElementById("box").onclick= function() {
    alert(clr);
}
<!DOCTYPE html>
    <html>
    <head>
        <title>

        </title>

    </head>
    <body>
      <div id="box" class="box" style="height: 100px;width: 100px;background-color: blue;margin: 0px;display: inline-block;"></div>
    </body>
    </html>

You can also use below code with provided html

  <script>
    var clr=  window.getComputedStyle(document.getElementById("box")).getPropertyValue('background-color'); // property
   document.getElementById("box").onclick= function() {
   alert(clr);
 }
 </script>

Comments

-1

Try using the variable clr inside the function. Also invoke the fucntion using onclick on the div itself. Using regex to get the property backgorund color in whole css. You can also use filter on #box using

var boxCss = clr.match(/#box{((.*\s*.*)*)}/g);


        <!DOCTYPE html>
        <html>
        <head>
            <title>

            </title>
    <script>

     function colorAlert() {
           var clr = document.getElementsByTagName("style")[0].innerHTML;   
    var res = clr.match(/background-color:.*;/g);
    alert(res[0]);
    }
    </script>
                <style>
                #box{

                            height: 100px;
                            width: 100px;
                            background-color: blue;
                            margin: 0px;
                            display: inline-block;
                    }
                </style>
        </head>
        <body>
            <div id="box" class="box" onclick="colorAlert();">

                    </div>
        </body>
        </html>

2 Comments

so this alerts blue right??? if not then remove the answer.
No, this helps me get comments of people who don't try but criticize.
-2

You have two ways to solve this :

  1. keep <script> tag after after closing <div> tag
  2. move clr var in side the onclick function,then you can write <script> tag where ever you want

1 Comment

did you understand the requirement?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.