0

I dont know why this code is not working! why is it showing the alert blank?

<html lang="en">

    <head>
        <title>Document</title>
        <style>
            #dd{
                height: 300px;
                width: 300px;
                background-color: mediumblue;
                color: white;
            }

        </style>
    </head>

    <body>
        <label>Color </label><input id="in1" type="text">
        <hr>

        <div id="dd">
    </div>

        <script>
            var colorTextBox = document.getElementById("in1");
            var div = document.getElementById("dd");

            div.onclick = function(){
                colorTextBox.value = div.style.backgroundColor;
        alert(div.style.backgroundColor);
            }

        </script>
       
    </body>
</html>

when the div is clicked the background color of the div should be written inside the input box and also shown in the alert.

1

2 Answers 2

0

Have this, it will compute the rgb color: `

    var colorTextBox = document.getElementById("in1");
                var div = document.getElementById("dd");

                div.onclick = function(){
                    //colorTextBox.value = div.style.backgroundColor;
            //alert(div.style.backgroundColor);
            
            const element = document.querySelector('#dd');
            const style = getComputedStyle(element)
            var color= style.backgroundColor;
            alert(color);
                }
    <html lang="en">

        <head>
            <title>Document</title>
            <style>
                #dd{
                    height: 300px;
                    width: 300px;
                    background-color: mediumblue;
                    color: white;
                }

            </style>
        </head>

        <body>
            <label>Color </label><input id="in1" type="text">
            <hr>

            <div id="dd">
        </div>

            <script>
                
            </script>
           
        </body>
    </html>

`

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

Comments

0

Element.style.<style> can only retrieve styles if it's inline.

For example, if you changed the div to be <div id="dd" style="background-color: mediumblue">, then div.style.backgroundColor will return mediumblue.

To get the background-color as defined in your style tag, you should use getComputedStyle(Element) instead. getComputedStyle(div).backgroundColor does work, returning rgb(0, 0, 205), which is the rgb value for mediumblue.

Demo

2 Comments

right can i some how get the name of the color? like mediumblue
@HassaanRazaNow This might help: stackoverflow.com/questions/5486730/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.