ubound
ubound

Reputation: 21

No style for results of my JavaScript quiz, why?

I produced this psychological test:

 <p>Q1: What do you think about XYZ?</p>
<input type="radio" name="group1" id="A1"> Agree strongly<br/>
<input type="radio" name="group1" id="E1"> Disagree very much<br/>
<p>Q1: What do you think about ABC?</p>
<input type="radio" name="group2" id="A2"> Agree strongly<br/>
<input type="radio" name="group2" id="E2"> Disagree very much<br/>
<button onclick="myFunction()">GET RESULTS</button>
<script>
counter = 0;
function myFunction() {
    if (document.getElementById('A1').checked){
    counter = counter - 2;
    } else if (document.getElementById('E1').checked) {
    counter = counter + 2;
    }
if (document.getElementById('A2').checked){
    counter = counter - 2;
    } else if (document.getElementById('E2').checked) {
    counter = counter + 2;
    }   
        if (counter > 0) {
        document.write("Its positive!");
        } else {
        document.write("Its negative!");
        }

}
</script>

When "GET RESULTS" button is clicked results are displayed properly but there is no CSS style applied. What can I do to make results appear on a styled page? Thank you!

Upvotes: 0

Views: 41

Answers (1)

slebetman
slebetman

Reputation: 113906

Because you used document.write.

When you use document.write the entire document, head, body, everything is replaced by whatever you write. Therefore you need to recreate a complete document if you use it.

The standard advise applies: never use document.write(). Learn about innerHTML instead (after that read the rest of the DOM API).

Upvotes: 1

Related Questions