Initialize variable

Asked

Viewed 46 times

1

I’m doing an event exercise that consists of typing a name into the input that will be printed below by pressing "send". It is then possible to apply new styles to this text, such as color, uppercase letter, increase font and hide and show the text again. The first time works perfectly, but when I click on "clean" and type another name, the new name already comes with the formatting of the previous text. I tried to initialize the variable using name = " ", but nothing happens. How can I fix it? (and excuse me if the title of the question is not very clear, I did not know exactly how to address this doubt)

<body>
Digite o nome que deseja animar: &nbsp; <input type="text" id="nome"><br>
<button onclick="nameAnimator()"> Enviar </button>
<button onclick="limpaNome()"> Limpar </button>
<p id="result"></p>
<button onclick="aumentarFonte()"> Aumentar </button>
<button onclick="mudaCor()"> Colorir </button>
<button onclick="letraMaiusc()"> Letra Maiúscula </button>
<button onclick="esconder()"> Esconder </button>
<button onclick="mostrar()"> Mostrar </button>

<script>
function nameAnimator(){  
    nome = document.getElementById("nome").value;
    document.getElementById("result").innerHTML = nome;
}
function limpaNome(){
    document.getElementById("nome").value = " ";
    document.getElementById("result").innerHTML = " ";
}
function aumentarFonte(){
    document.getElementById("result").style.fontSize = "35px";
}
function mudaCor(){
    document.getElementById("result").style.color = "red";
}
function letraMaiusc(){
    document.getElementById("result").innerHTML = nome.toUpperCase();
}
function esconder(){
    document.getElementById("result").innerHTML = "";
}
function mostrar(){
    document.getElementById("result").innerHTML = nome;
}
</script>
   </body>
  • Just reset the values in the function cleanName(), example: document.getElementById("result").style.color = "black"; &#xA; document.getElementById("result").style.fontSize = "20px";

1 answer

1


You are applying styles (attribute style) to the element <p> with the id #result. To get back to normal you can do it in two ways:

1) Empty the attribute style: document.getElementById("result").style = "";

or

2) Remove the attribute style (recommended): document.getElementById("result").removeAttribute("style");

Browser other questions tagged

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