How to update HTML text by button?

this is what i achieved so far. With the button i can get scores to add up to total. but at the Score it is not updated everytime.

 <script>
    function True(){
        score5=1;
            alert("Correct");   
        total=score1+score2+score3+score3+score5;
        }
    </script>

    <input type="button" value="True" onClick="True()">

    <h2>Score:<script>document.write(total)</script><h2>

Answers


The document.write will run when the page is being parsed by the browser, way before the True() function is run. You need to give an id to your h2, then use that to set its innerHTML to the calculated score inside True():

// dummy values
var score1 = 5, score2 = 4, score3 = 3, score4 = 2;

function True(){
    score5=1;
    alert("Correct");   
    total=score1 + score2 + score3 + score4 + score5;
    document.getElementById('score').innerHTML = "Score: " + total.toString();
}
<input type="button" value="True" onClick="True()" />

<h2 id="score">Score: 0<h2>

Need Your Help

How to replace text to make class of a div

javascript jquery html replace addclass

Trying to replace text to class of a div. Means find text from .label (text1 or any text), then replace it as class class="text1" to parent list &lt;li&gt; item like &lt;li class="text1"&gt;. Example

platform-independent /dev/null output sink for Java

java stream

Other than an anonymous class (new OutputStream() { ... }), can anyone suggest a moral equivalent of new FileOutputStream("/dev/null") that also works on Windows?

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.