...

View Full Version : need Help with a Variable string change issue(javascript with if else problem)



xkai
05-05-2008, 03:33 PM
Hi

i am new to the forum, i came here to hopefully gain some help and advise on how to fix this problem with variable string change that i have.

I started doing two classes in my advance diploma and they are a HTML(client side programming) and Java(object oriented Programming) class.

This webpage with multi choice question is from my assignment and i think i am missing something in the if else statements or some else that i don't know.

i am still got alot to learn, and my teacher gives me answer on how to fix it in riddles, which is sorta weird.

The problem is i cant seem to get the if else statements to change the inside alert messages which are string1 and string2, to string3 and string4 on the onclick nextquestion button.

here the code:

<html> <head> <title> Multi-Choice Java Question WebPage!</title>
<script language="JavaScript">
var string1="Correct Answer";
var string2="Incorrect Answer (The Correct Answer is C)";
var string3="Incorrect Answer (The Correct Answer is D)";
var string4="Correct Answer";
var Question3=string3;
var Question4=string4;
var number=0;

function ChangeQuestion()
{
document.getElementById('Question').innerHTML='How Many Cores are in a Intel Core 2 Quad Q6600 ?';
}
function A()
{
document.getElementById('QA').innerHTML='One';
}

function B()
{
document.getElementById('QB').innerHTML='Two';
}

function C()
{
document.getElementById('QC').innerHTML='Three';
}

function D()
{
document.getElementById('QD').innerHTML='Four';
}

function number()
{
number=1
}

if (number ==0)
{
document.getElementById('string1').innerHTML='string1';
document.getElementById('string2').innerHTML='string2';
}
else
{
document.getElementById('string1').innerHTML='string3';
document.getElementById('string2').innerHTML='string4';
}



</script>

</head>
<body background="java1.jpg">
<body>

<h2><p> <b id='Question'> # JFrame Class Is Found In What Package? #</b> </p></h2>

<form>
<table>

<h3><tr><input type="button" value="a)" onclick='alert("Incorrect Chosen Answer (The Correct Answer is C)")'><b id='QA'>java.awt</b></tr>

<tr><input type="button" value="b)" onclick='alert("Incorrect Chosen Answer (The Correct Answer is C)")'><b id='QB'>java.awt.event</b></tr>

<tr><input type="button" value="c)" onclick='alert(string1)'><b id='QC'>java.swing</b></tr>

<tr><input type="button" value="d)" onclick='alert(string2)'><b id='QD'>java.io</b></tr>

<tr><input type="button" value="Next Question" onclick='ChangeQuestion(), A(), B(), number(), C(), D()'>

</h3>
</table>
</form>

</body></html>

Thank You

A1ien51
05-05-2008, 04:07 PM
The if statement is called when the page is rendered. It is not called with a function.

Eric

xkai
05-05-2008, 04:14 PM
I know i just wonder if someone could fix my code cause i cant get those string1 and string2 to change if you could help with writting the code plz do thank you .

Philip M
05-05-2008, 06:29 PM
You should be aware that Java and Javascript are entirely different programming languages, in spite of the confusingly similar names.

This code:-

if (number == 0)
{
document.getElementById('string1').innerHTML='string1';
document.getElementById('string2').innerHTML='string2';
}
else
{
document.getElementById('string1').innerHTML='string3';
document.getElementById('string2').innerHTML='string4';
}

needs to be contained within a function. As it is it is executed immediately as soon as the page loads.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum