Changing colors when hovering the mouse

Asked

Viewed 1,295 times

1

The following code is able to perform the following task:

  • By clicking the "Create Square" button, a red square is rendered on the screen at each click.

I wish that, when passing the mouse over these generated squares, these change color.

Follows the code :

<html>
      <head>
        <meta charset="UFT-8" />
        <title></title>
      </head>

      <body>
        <div id="container"></div>
        <button type="button" onclick="criarQuadrado()">Criar quadrado</button>
        <script>
          function criarQuadrado() {
            var boxElement = document.createElement("div");
            boxElement.style.width = 300;
            boxElement.style.height = 300;
            boxElement.style.display = "inline-block";
            boxElement.style.backgroundColor = "#f00";

            var container = document.getElementById("container");
            container.appendChild(boxElement);
          }
    </script>
      </body>
    </html>

The function either will be responsible for generating the color changes is as follows :

function getRandomColor() {
        var letters = "0123456789ABCDEF";
        var color = "#";
        for (var i = 0; i < 6; i++) {
          color += letters[Math.floor(Math.random() * 16)];
        }
        return color;
}
var newColor = getRandomColor();

How can I solve such a problem?

  • You and you must associate the color change function to the onfocus event.

  • But the color change is permanent or should take effect only while the user has the mouse over the square ?

2 answers

2

Follow code below, tested and every time you put the mouse on top of the square it changes color, I do not know if this is exactly what you want, but it is already a way to get there.

<html>
  <head>
    <meta charset="UFT-8" />
    <title></title>
  </head>

  <body>
    <div id="container"></div>
    <button type="button" onclick="criarQuadrado()">Criar quadrado</button>
    <script>
      function criarQuadrado() {
        var boxElement = document.createElement("div");
        boxElement.style.width = 300;
        boxElement.style.height = 300;
        boxElement.style.display = "inline-block";
        boxElement.style.backgroundColor = "#FF0000";
        boxElement.addEventListener('mouseover', function(){
            var letters = "0123456789ABCDEF";
            var color = "#";
            for (var i = 0; i < 6; i++) {
            color += letters[Math.floor(Math.random() * 16)];
            }
            return this.style.background = color;
        });

        var container = document.getElementById("container");
        container.appendChild(boxElement);
      }
</script>
  </body>
</html>

0

If I get it, you want to change the colors when passing the mouse, right?

I believe you have to create a function that takes the value of getRandomColor() and compare her.. kind of like this:

function mudarDeCor(){
          if(newColor === newColor){
            newColor = getRandomColor();
            return newColor;
          } 
        }

I believe that’s how it is. Take the test

  • "Do you test your code? Or do you send it to production and wait for the user to report a Bug and then do it right?" Don’t take it personally, it’s a constructive criticism ok, before posting an answer and having the guy test, test you, and see if it’s worth posting as an answer something you don’t know if it will work.

  • 1

    In if(newColor === newColor){ there is the possibility of the result being false?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.