Show result inside a div without load on the page

Asked

Viewed 110 times

-1

I created a home screen where you put the value you want to be displayed on the table. However, when you click the button it jumps to another screen.. wanted him to display on the same screen that is already stylized.

How can I do it? I’m using Document.write...

Code:

<div class="calculadora">

    <h1>Digite o número que deseja saber a tabuada :</h1> <input type="text" maxlength="2" id="txtNumero">
    <button onclick="calcular()">Calcular</button>

    <div id="resultado">
    </div>
</div>

<script>



    function calcular() {
        numero = document.getElementById("txtNumero").value;


        for (i = 0; i < 11; i++) {

            document.write(`<br>${numero} X ${i} = ${numero * i} <br>`);
        }

    }

</script>

IMAGEM DO CODIGO

2 answers

1

You are not "jumping" to another screen, you are overwriting the existing one with document.write.

If you don’t want to lose your page’s HTML, create an element like a div, and then attach your text to that element. I even believe that in your code, this div resultado has already been created for this purpose, no?

function calcular() {
    var numero = document.getElementById("txtNumero").value;
    var elemResultado = document.getElementById("resultado");
    elemResultado.innerHTML = '';

    for (var i = 0; i < 11; i++) {
        elemResultado.innerHTML += `<br>${numero} X ${i} = ${numero * i} <br>`;
    }
}
  • I really appreciate @user140828 gave exactly how I wanted it. I will study a little more about innerHTML. First time I’m using the stack overflow and it was really good. I will help the community too.

0

You are not skipping to another page note your URL is the same. The logic problem is that you are using the method write() this method it is used most of the time for testing and not for projects like the one you made, because if an html document is completely rendered by the browser and write() is present he will completely delete your document. It was your case you added html to your page and then called the white() and resulated it deleted your html. Use this method only for small test to solve this created this simple solution.

<!DOCTYPE html>
<html lang="pt-br">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <div class="calculadora">
    <h1>Digite o número que deseja saber a tabuada :</h1> <input type="text" maxlength="2" id="txtNumero">
    <button onclick="calcular()">Calcular</button>
  </div>
<script>
    function calcular() {
        numero = document.getElementById("txtNumero").value;
        
        for (i = 0; i < 11; i++) {
            var p = document.createElement("p");
            document.body.appendChild(p);
            p.innerHTML = (`<br>${numero} X ${i} = ${numero * i} <br>`);
        }
    }
</script>
</body>
</html>

Hardly changed at all I just did that every time for run it will create a paragraph and add in body and the result will be implemented in the paragraph :).

  • Perfect Leandro Birth, I tested here and gave right too. very good logic. I thank you very much. ! vc is fera!

  • 1

    Vlw, it is nois! :)

Browser other questions tagged

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