Display JS variable in HTML

Asked

Viewed 69 times

-1

Speak people, I have a js variable that counts the number of points based on a questionnaire, it counts the points correctly, just do not know how I can display this variable value in my html, someone can help please?

Here is my js

function contarPontos() {
    if (!localStorage.pontos) {
        localStorage.pontos = 0;
    }
    localStorage.pontos = parseInt(localStorage.getItem("pontos")) + 10;
}

And here’s my HTML where I want to display

                    <div class="mu-title">
                        <span class="style-texto-conclusao">Sua pontuação foi pontos!</span>
                        <div id="contarPontos"></div>
                    </div>

1 answer

1

In your case just use the innerHTML to display the score. Simply retrieve the score and assign it to the element with the id=contarPontos.

function contarPontos() {
    if (!localStorage.pontos) {
        localStorage.pontos = 0;
    }
    localStorage.pontos = parseInt(localStorage.getItem("pontos")) + 10;
    document.getElementById("contarPontos").innerHTML = localStorage.pontos;
}

If you are using jQuery, can do so

$("#contarPontos").text(localStorage.pontos)

Browser other questions tagged

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