How to return a Javascript calculation to the same page?

Asked

Viewed 58 times

2

I would like to know how to make a calculation made in the call of a function in Javascript return me on the same page, because when I do the action to perform the calculation, it returns me the values on a different page.

Follows structure of HTML.

<html>
    <head>
    <meta charset = "UTF-8">
        <title>  Calculo  </title>
        <script src ="calcularMedia.js">  </script> 

    </head>
            <body>
                    <h2 align = "center"> Cálcuo 1: bimestre  </h2>
                    <form name = "f1"> 
                    <p align = "left">
                    Disciplina: 
                    <input type = "text" name = "disciplina" >
                    <p align = "left">
                    Nota: 
                    <input type = "text" name = "nota">
                    <input type = "button" name = "calcular" value = "Calcular" onclick = "calcularMedia()">            

            </body>


</html>

Code Avascript

function calcularMedia(){

    disciplina = f1.disciplina.value;
    nota = f1.nota.value;   
    media = parseFloat(nota) * parseFloat(0.4);


    document.write("<p>Disciplina: " + disciplina+"</p>");
    document.write("Media:<font color = #FF0000> "+media+"</font>");


}
  • Play the result in a div or input do not use the document.write

  • It would be possible to show me how I do this process in code?

  • 1

    Create a div with the id called result, in your function do, document.getElementById('resultado').innerHTML = media;

  • 1

    It worked here. Thank you.

No answers

Browser other questions tagged

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