Innerhtml does not work

2

I'm trying to make a very simple javascript example but innerhtml is not being recognized.

<!DOCTYPE html>
<html>

    <head>

            <title>Pagina com Javascript</title>
            <script src="meujavascript.js"></script>
            <!-- Versao do JS <script type="text/javascript" language="JavaScript1.8.5"></script> -->

    </head>

    <body>
    <!--
    <p id="paragrafo">texto em javascript</p>
    <button type="button" onclick="mostrar()">Teste</button>
    -->

    <p id="valores">[valores]</p>

    </br></br></br>

    <button type="button" onclick=adicionar(1,2)">Mensagem</button>

    </body>

</html>

javascript function:

function adicionar(parcela1, parcela2)
{
    //esta função adiciona dois valores
    var resultado = parcela1 + parcela2;
    var test document.getElementById("valores");
    test.innerhtml = resultado;
}

Very simple HTML just to practice.

My javascript example. I want to continue to practice but I do not know why the innerhtml is not working .. Can anyone help?

    
asked by anonymous 13.10.2016 / 15:37

3 answers

6

Only 2 errors in JavaScript code:

  • var test document.getElementById("valores"); : After 'var test' could have been declared '=' (initializer), ',' (next variable), ';' or a line break, but instead a name appeared.

  • Your code is interpreted and executed in case-sensitive, so test.innerhtml will not be equivalent to test.innerHTML . test.innerHTML = ... is the setter you want to use.

To solve the code problems, I declare an initializer in front of the definition of the 'test' variable and changed test.innerhtml to test.innerHTML , which can be the getter / setter HTMLElement#innerHTML in today's browsers.

function adicionar(parcela1, parcela2) {
    var resultado = parcela1 + parcela2;
    var test = document.getElementById("valores");
    // Se a propriedade test.innerHTML tem um setter em test,
    // chama a função do setter, se não então re-define
    // a propriedade test.innerHTML sensivelmente
    test.innerHTML = resultado;
}
    
13.10.2016 / 16:09
3

Use:

var test = document.getElementById("valores"); // falta o '='
test.innerHTML = resultado                     // 'html' em maiúsculo

link

    
13.10.2016 / 16:03
1

Capital letters in innerHTML and quotation marks

        

    <head>

    <title>Pagina com Javascript</title>
    <script src="meujavascript.js"></script>
    <!-- Versao do JS <script type="text/javascript" language="JavaScript1.8.5"></script> -->
    <script>
    function adicionar(parcela1, parcela2)
    {
    //esta função adiciona dois valores
    var resultado = parcela1 + parcela2;
    var test = document.getElementById("valores");
    test.innerHTML = resultado;

    }
    </script>
    </head>

    <body>
    <!--
    <p id="paragrafo">texto em javascript</p>
    <button type="button" onclick="mostrar()">Teste</button>
    -->

    <p id="valores">[valores]</p>
    <p id="demo"></p>
    <br><br><br>

    <button type="button" onclick="adicionar(1,2)">Mensagem</button>

    </body>

    </html>

link

    
13.10.2016 / 16:44