www.webdeveloper.com
Results 1 to 9 of 9

Thread: if else help!

  1. #1
    Join Date
    Oct 2011
    Location
    Hamilton, Ontario
    Posts
    81

    if else help!

    the problem I have with the following script was that no matter what response the result still came out as "Nope!"... any ideas?

    function ques1() {
    var x=document.forms['ques1'].elements['response1'];
    if (x==2) {
    document.forms['ques1'].elements['result1'].value="Correct!";
    }
    else {
    document.forms['ques1'].elements['result1'].value="Nope!";
    }
    }

  2. #2
    Join Date
    Oct 2011
    Location
    Hamilton, Ontario
    Posts
    81
    i also tried

    function ques1() {
    var x=document.forms['ques1'].elements['response1'];
    if (x="2") {
    document.forms['ques1'].elements['result1'].value="Correct!";
    }
    else if (x!="2"){
    document.forms['ques1'].elements['result1'].value="Nope!";
    }
    }

    and instead it says "correct!" no matter what i put in

  3. #3
    Join Date
    Nov 2010
    Posts
    1,082
    kinda hard to say without knowing what
    Code:
    document.forms['ques1'].elements['response1']
    is.

    although
    Code:
    if (x="2")
    will never work, but
    Code:
    if (x=="2")
    might

  4. #4
    Join Date
    Oct 2011
    Location
    Hamilton, Ontario
    Posts
    81
    i tried that and it still puts "Nope!" in the result box no matter what input is in the response field... but here is the whole thing..

    <script type="text/javascript">
    function ques1() {
    var x=document.forms['ques1'].elements['response'];
    if (x=="2") {
    document.forms['ques1'].elements['result'].value="Correct!";
    }
    else if (x!="2"){
    document.forms['ques1'].elements['result'].value="Nope!";
    }
    }
    </script>
    <form action="Javascript:ques1();" name="ques1">
    <font size="5"><b><i>HOW MANY
    <img src="http://melodicpulse.webs.com/pics/exercises/qnote.jpg" width="50"/>==
    <img src="http://melodicpulse.webs.com/pics/exercises/hnote.jpg" width="50"/>?</i></b></font>
    <input type="text" name="response" value="type answer here" />
    <input type="text" name="result" value="result appears here" />
    <input type="submit" name="submit" value="Submit" />
    <input type="reset" name="reset" value="Reset Form" />
    </form>

    still doesn't work right

  5. #5
    Join Date
    Nov 2010
    Posts
    1,082
    Code:
    var x=document.forms['ques1'].elements['response'].value;
    if (x=="2") {
    //etc...

  6. #6
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Your problem is that you try to use a submit button, and the submit action to do something in JavaScript, which is an error. The submit process changes the session, and any changing of the session will make all the javascript calculation lost.

    You may block the submit action by returning false:

    Code:
    function ques1() {
    // whichever code
    return false
    }
    ...
    ...
    <form action="" name="ques1" onsubmit="return ques1()">
    Or you may simply use a type="button" input, not a submit one:
    Code:
    <form action="" name="ques1">
    ...
    <input type="button"  value="Submit" />
    </form>

  7. #7
    Join Date
    Jan 2011
    Posts
    117
    Quote Originally Posted by xelawho View Post
    Code:
    var x=document.forms['ques1'].elements['response'].value;
    if (x=="2") {
    //etc...
    Yap, that's how it's done,
    -can't compare a "DOM Element" to a "Number", or a "String" instead of its "value" and hope for the best.

  8. #8
    Join Date
    Dec 2003
    Location
    Bucharest, ROMANIA
    Posts
    15,428
    Quote Originally Posted by Troy III View Post
    Yap, that's how it's done,
    -can't compare a "DOM Element" to a "Number", or a "String" instead of its "value" and hope for the best.
    Agree. But in his particular case that is not enough (see my comment above)

  9. #9
    Join Date
    Nov 2010
    Posts
    1,082
    Quote Originally Posted by Kor View Post
    Agree. But in his particular case that is not enough (see my comment above)
    actually, weird as it is, in this case it is enough (works in FF, Chrome & IE at least)... look at the form action... it seems the antiquated "javascript:" blocks the submission action

    but I would agree - there's no point in a submit button that is not actually submitting anything.

    on a side note, the function could easily be simplified using the ternary...

    Code:
    function ques1() {
    var x=document.forms['ques1'].elements['response'].value;
    document.forms['ques1'].elements['result'].value=x=="2"?"Correct!":"Nope!";
    }

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles