Create red square on the screen - I need to create a button, where every time it is pressed, a red square is created

Asked

Viewed 80 times

1

Good morning,

I need to create a button, where every time it is pressed, a red square is created on the screen. I tried to do this with the code below, but this way the button is generated only once. Could someone help me?

Create Red Square
<script>
    var btnElement = document.querySelector('button.botao');

    btnElement.onclick = function() {
            btnElement.style.width = '100px';
            btnElement.style.height = '100px';
            btnElement.style.backgroundColor = '#f00';
        
    }        
</script>
  • You can create an html element and format as you wish with CSS (red square) and leave it display:hidden. When you click the button, you switch to display: visible.

  • Thank you for the reply Peter! I will try here.

2 answers

0

You can use document.createElement in this way:

<div id="meuDiv"><!--Aqui será criado o quadrado--></div>
<button onclick="myFunction()"><!--Toda vez que apertar esse botão criará um quadrado-->Me aperte</button>
<script>
function myFunction(){
    var quadrado = document.createElement("DIV");//crio uma variável para definir que o elemento será uma div
    quadrado .innerHTML = "<h1>Hello World</h1>";//crio uma tag H1(com "hello world" escrito nela) dentro da div
    document.getElementById("meuDiv").appendChild(quadrado);//pego o nome da div pai usando seu id("meuDiv") depois uso appendChild para criar um elemento filho dentro da div
}
</script>

You can also now make it have the specifications you want! For this you can do in the css itself and add with DOM a class for example, or go adding one by one with DOM like this quadrado.style.(aqui fica a especificação css) = "(aqui o valor atribuído)".

Of course it’s not patched that way. But it has a better way, which is to add this code quadrado.classList.add("minhaClass") in function myFunction() then add your CSS specifications to a CSS sheet.

  • Very good explanation, thank you! I managed to solve, and now I have another challenge rs Now I need to change the color of the square when I hover over it. Could you help me again? I tried that way and I couldn’t.

  • is simple in css put there (element):{/here are the specifications/} ":Hover" causes css to check every time you hover over an element, in case you want to change the color use a background-color: (color here)

  • Hello, well, there are two ways you can try again! first, in css: . square:Hover{background-color: (new color)} second, in js: you can use onmouseout by placing it in the element or in an addeventlistener, create a function and within it put DOM(element).style.background = "(new color)"; - I suggest you give an in-depth study on "css :Hover" and "DOM" in js

-1

 <>
  <button id="toggleButton">toggle display element</button>
  <div
    id="square"
    style={{
      textAlign: "center",
      margin: "10px",
      color: "white",
      backgroundColor: "red",
      width: "100px",
      height: "100px",
    }}
  >
    square
  </div>
</>

jquery:

$(Document). ready(Function() { $("#toggleButton"). click(Function(){ $("#square"). toggle(); }); });

Browser other questions tagged

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