Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    May 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    need Help with a Variable string change issue(javascript with if else problem)

    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

  • #2
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    The if statement is called when the page is rendered. It is not called with a function.

    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • #3
    New to the CF scene
    Join Date
    May 2008
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 .

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,017
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    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.
    Last edited by Philip M; 05-05-2008 at 06:35 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •