Click here to Skip to main content
15,923,789 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Can anybody please help me?

I have written a function in javascript:
C#
<script language="JavaScript">
    var count = 0;
    function setText(){
        if(document.check.jawapan.value == "A"){
            alert("Tahniah. Jawapan anda betul");
            count = 1;
        }
        else
        {
            alert("Jawapan anda salah. Sila cuba lagi");
            count = 0;
        }
        document.write(count);
    }
</script>



I have created a link to run the function.
<input type=BUTTON value="Semak" name="mySubmit" önClick="setText()">


After I click the button the value prints on the new page and not in same page. Did I miss some code or can anybody suggest what I'm doing wrong?

Thanks in advance!
Posted
Updated 28-Mar-11 23:21pm
v2

document.write(...) will do exactly what you are observing. It writes some text into to the document body and if the body had already been loaded this will cause the body to be completely overwritten. If one executes an inline script function while the document is being loaded the behaviour is quite different in that document.write will add to the text already loaded.
This behavior has always been causing confusion, but now that you know about it it should no longer be an issue.
:)

Cheers and happy coding!
 
Share this answer
 
Hi
Why are you print Count value using document.write?

you can write following in HEAD
XML
<script language="JavaScript">
    var count = 0;
    function setText(){
        if(document.check.jawapan.value == "A"){
            alert("Tahniah. Jawapan anda betul");
            count = 1;
        }
        else
        {
            alert("Jawapan anda salah. Sila cuba lagi");
            count = 0;
        }
        document.getElementById('divOutput').innerHTML = count;
    }
</script>


you can use as following in HTML body
XML
<div id="divOutput"></div>
<input type="button" value="semak"  önclick="setText()" />
 
Share this answer
 
v2
Comments
LearningPerson 29-Mar-11 12:46pm    
thank for your codes... i'm appreciated it... :)
LearningPerson 29-Mar-11 12:51pm    
by the way.... i just want to ask you... can i know what divOutput used for..?
Sunasara Imdadhusen 30-Mar-11 0:29am    
divOutput will show the out put of Count value. by the you can take any Div to display value.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900