Turn on thread page Beta

help with javascript watch

    • Thread Starter
    Offline

    3
    ReputationRep:
    cant figure out what's wrong with this (as it doesnt work) (see attachement)

    I am trying to calculate the scores of al the options selected and depending on the score another page will be displayed which will show a description of the person who filed in the form...
    Attached Files
  1. File Type: txt js_code.txt (6.9 KB, 71 views)
    Offline

    15
    ReputationRep:
    this line (and its clones) looks really dodgy

    Code:
    for (i = 0; i < f.one.length; i++) if (f.one[i].checked) value = f.one[i].value;
    Offline

    15
    ReputationRep:
    (Original post by elpaw)
    this line (and its clones) looks really dodgy

    Code:
    for (i = 0; i < f.one.length; i++) if (f.one[i].checked) value = f.one[i].value;
    change it to this

    Code:
    for (i = 0; i < f.one.length; i++){ if (f.one[i].checked){ value = f.one[i].value}};
    Offline

    15
    ReputationRep:
    (Original post by piginapoke)
    Those lines do the same thing. You can omit braces after if and for statements and the first line only will be treated as being the body of the if or for statement. Bad practice, but it works.
    but it didnt work until i changed it. like you said, the first line after it gets traeated, not what comes directly after it (without linebreak), and it just gets confused
    • Thread Starter
    Offline

    3
    ReputationRep:
    (Original post by elpaw)
    change it to this

    Code:
    for (i = 0; i < f.one.length; i++){ if (f.one[i].checked){ value = f.one[i].value}};

    it still says error on page
    • Thread Starter
    Offline

    3
    ReputationRep:
    (Original post by piginapoke)
    It works perfectly well without modification on my IE and firefox, the only thing missing is a check to see if all the boxes are checked. If you miss one out, you get an error (value is undefined).

    Just put a check in to make sure all of the options have been selected.
    Why doesnt it work on my machine then? :confused:
    • Thread Starter
    Offline

    3
    ReputationRep:
    (Original post by piginapoke)
    Did you put an answer for each of the questions?
    Well i assigned a score for each answer so it al adds up and depending on the final score it displays the appropriate page..
    • Thread Starter
    Offline

    3
    ReputationRep:
    (Original post by piginapoke)
    Yes I see that. The only time I can get it to produce an error is when I press the button at the bottom without selecting any answers. That way, 'value' is undefined and an error is produced. But if answers are selected, there's no problem, it redirects me to another page (e.g. gr.html)
    this is so bloody strange it..whne i select all the answers it doesnt redirect me anywhere instead i sit ont he same oage (with an error at the bottom)
    i dont understand
    • Thread Starter
    Offline

    3
    ReputationRep:
    (Original post by piginapoke)
    Have a look at what the error says (click on the yellow exclamation mark), it will tell you what is going on.

    Error Message:

    Line: 14
    Char: 14
    Error: 'one' is null or not an object
    Code: 0
    URL: file//c:\folder\js.htm
    • Thread Starter
    Offline

    3
    ReputationRep:
    (Original post by piginapoke)
    Is that when you click on the button, or just when the page loads?

    Try referring to f as 'document.forms["f"]', and f.one as 'document.forms["f"].one' and so on.
    when i click on the button
    STILL GIVES ME THE SAME ERROR
    • Thread Starter
    Offline

    3
    ReputationRep:
    (Original post by piginapoke)
    Is that when you click on the button, or just when the page loads?

    Try referring to f as 'document.forms["f"]', and f.one as 'document.forms["f"].one' and so on.
    im still getting the same error..
    • Thread Starter
    Offline

    3
    ReputationRep:
    (Original post by piginapoke)
    Might no be the cause but you haven't closed the <form> tag with </form>.

    well i have now but still the same error..errrrrr :mad: stupid error driving me mad
    • Thread Starter
    Offline

    3
    ReputationRep:
    (Original post by piginapoke)
    Stick this at the top with the other variable declarations:

    var value = 0;
    same error message
    • Thread Starter
    Offline

    3
    ReputationRep:
    (Original post by piginapoke)
    Maybe it doesn't like the word 'value' as a variable name. You could change every instance where you've used 'value' as a variable to something else an give that a whirl.

    same message...im gonna commit suicide :eek:
    Offline

    0
    ReputationRep:
    I think I see what's happening here. When a radio is not checked, the code does not enter the if condition following the for loop, and hence 'value' is not ever defined. Therefore, when you check the value of 'value', with these lines,
    Code:
    if (value == "1") { hu++; }
    if (value == "2") { sl++; }
    if (value == "3") { ra++; }
    if (value == "4") { gr++; }
    it is not defined and the browser's javascript interpreter has a fit. As far as I can tell, you only want points to be added if the user has checked a box, so this code should sort you out. It's basically the same as yours, except it only adds to the users score (and hence uses 'value') if they have checked a box. You could easily add an else clause to the if to alert them they forgot a question, or stop processing, etc etc.

    Anyway - try this:

    Code:
    for (i = 0; i < f.one.length; i++) 
    {
        if (f.one[i].checked) 
        {
            value = f.one[i].value;
    	if (value == "1") { gr++; }
    	if (value == "2") { ra++; }
    	if (value == "3") { sl++; }
            if (value == "4") { hu++; }
        }
        else
        {
             // didn't choose an option!
        }
    }
    Also, for correctness and compliance with the DOM specification, you should define an id for your form - like:
    Code:
    <form name="f" id="my_form">
    ...
    </form>
    and then in your javascript, define your variable f like this:
    Code:
    var f = document.getElementById("my_form");
    That should work - and as someone said earlier, please indent your code and use braces, even for one-line blocks! It makes it much easier to read and follow...

    Cheers,

    Ben
    • Thread Starter
    Offline

    3
    ReputationRep:
    it works but it keeps redirecting me to only one page no mater what options i select..
    Offline

    0
    ReputationRep:
    Hmmm, I just tested the code on my machine here (IE6 and FF0.9.2) and it seems to work alright - seems to be trying to load different pages dependant on options. It may sound silly but make sure you've changed the f.one in the code above to f.two, f.three ....etc where appropriate.

    Failing that, could I see the code that isn't working?

    Ben
    • Thread Starter
    Offline

    3
    ReputationRep:
    (Original post by karatesasquatch)
    Hmmm, I just tested the code on my machine here (IE6 and FF0.9.2) and it seems to work alright - seems to be trying to load different pages dependant on options. It may sound silly but make sure you've changed the f.one in the code above to f.two, f.three ....etc where appropriate.

    Failing that, could I see the code that isn't working?

    Ben
    these are the codes...
    Attached Files
  2. File Type: txt js_code.txt (6.9 KB, 56 views)
    Offline

    0
    ReputationRep:
    Here you go. Tested on IE6 and FF0.9.2 - I think it's what you're after.

    (Although, all I did was put the code that I posted above in ...! =P)

    Let me know if you need any more help ...

    --
    Ben
    Attached Files
  3. File Type: txt js_code_fixed.txt (7.0 KB, 77 views)
 
 
 
Poll
Who is most responsible for your success at university

The Student Room, Get Revising and Marked by Teachers are trading names of The Student Room Group Ltd.

Register Number: 04666380 (England and Wales), VAT No. 806 8067 22 Registered Office: International House, Queens Road, Brighton, BN1 3XE

Write a reply...
Reply
Hide
Reputation gems: You get these gems as you gain rep from other members for making good contributions and giving helpful advice.