Innerhtml does not work

Asked

Viewed 1,615 times

2

I’m trying to make a very simple example of javascript but the 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 for practice.

My javascript example. I want to continue practicing but I don’t know why innerhtml isn’t working.. Someone can help me ?

  • 1

    innerhtml must be with HTML great. => innerHTML

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 shall not be equivalent to test.innerHTML. test.innerHTML = ... is the Setter you want to use.

To solve the problems of the code I declared an initializer in front of the variable definition 'test' and traded test.innerhtml for test.innerHTML, which may 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;
}

3

  • But isn’t that what he’s doing? In the statement of the question, it has the same code..

  • @Marcogiovanni Not really. The parser is case-sensitive: test.innerhtml is different from test.innerHTML.

  • Well, I hadn’t noticed that kkkk .. + 1

  • 1

    You didn’t even notice the = missing there in the variable test right!

  • Truth @Sampaioleal ..

0

Uppercase 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>

https://plnkr.co/edit/0zixeHtok0L3QORwmggy?p=preview

  • Welcome to stack overflow in English, please translate your answer.

Browser other questions tagged

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