IF statements URGENT!!! PLEASE HELP :)

From C++ to PHP, debugging to webhosting; help and discussion about writing your latest program to running your website. NOT for help when your PC won't work.

Announcements Posted on
TSR launches Learn Together! - Our new subscription to help improve your learning 16-05-2013
Sign in to Reply
  1. mfaxford's Avatar
    • Overlord in Training
    • Location: Southampton
    • Posts: 2,117
    Re: IF statements URGENT!!! PLEASE HELP :)
    (Original post by Sparrow_Squire)
    this is current JS
    Code:
    function testButton (form){
            	if 
            		(document.testform.getElementByID("pCar").checked
                                    {
    				document.write("gah");
    				}
    			else
    				(form.getElementByID("dCar").checked
    				{
    				document.write("blah");
    				}
    				}
    Fixed by adding [ CODE ] and [ /CODE ] tags.

    There's the unclosed brackets that's already been mentioned.

    Also else doesn't take a parameter so you either want
    Code:
    if (something) {
        action;
    } else {
       other_action;
    }
    or
    Code:
    if (something) {
        action;
    } else if (something_else) {
       other_action;
    }
    the final else statement is taken if none of the preceding actions are taken.
  2. Sparrow_Squire's Avatar
    • Junior Member
    • Location: Huddersfield
    • Posts: 58
    Re: IF statements URGENT!!! PLEASE HELP :)
    (Original post by mikeyd85)
    Now, I'm no dev at all (my experience with ifs come from Excel), but I'd say that bringing in form.rad[1].checked as part of the else statement is superfluous. I would have thought that you'd need a nested if statement to make that work, as well as a third output.
    this was 1st attempt it worked but only for the first button iv posted a newer version since x



    (Original post by ANARCHY__)
    Why document.testform on one line and then form on the other? Surely this would be calling from a different form?
    im not sure it just seemed to work better like that. ive just tried it as document.form and altered form name to form thats not working either?
  3. ANARCHY__'s Avatar
    • Overlord in Training
    • Posts: 2,530
    Re: IF statements URGENT!!! PLEASE HELP :)
    (Original post by Sparrow_Squire)
    this was 1st attempt it worked but only for the first button iv posted a newer version since x





    im not sure it just seemed to work better like that. ive just tried it as document.form and altered form name to form thats not working either?
    But if you're calling form, that's not correct (as far as I can see) because you're calling an entirely different object where the radio button is not present.
  4. Sparrow_Squire's Avatar
    • Junior Member
    • Location: Huddersfield
    • Posts: 58
    Re: IF statements URGENT!!! PLEASE HELP :)
    (Original post by ANARCHY__)
    But if you're calling form, that's not correct (as far as I can see) because you're calling an entirely different object where the radio button is not present.
    oh. ... what do you recommend?
  5. ANARCHY__'s Avatar
    • Overlord in Training
    • Posts: 2,530
    Re: IF statements URGENT!!! PLEASE HELP :)
    (Original post by Sparrow_Squire)
    oh. ... what do you recommend?
    Well, I'd copy and paste your first part of the if statement and then replace the else with this, amending the parts you need to to make it work for the 'else' portion.
  6. Sparrow_Squire's Avatar
    • Junior Member
    • Location: Huddersfield
    • Posts: 58
    Re: IF statements URGENT!!! PLEASE HELP :)
    confused lol you mean;

    <script type="text/javascript" language="javascript">
    function testButton (form)
    {
    for (Count = 0; Count < 3; Count++)
    {
    if (form.rad[0].checked)
    break;
    }
    alert ("Button " + Count + " is selected");
    {
    else (form.rad[1].checked)
    break;
    }
    alert ("Button" + Count + "is selected")
    }
  7. Fried Sock's Avatar
    • Full Member
    • Location: Preston
    • Posts: 97
    Re: IF statements URGENT!!! PLEASE HELP :)
    (Original post by Sparrow_Squire)
    confused lol you mean;

    <script type="text/javascript" language="javascript">
    function testButton (form)
    {
    for (Count = 0; Count < 3; Count++)
    {
    if (form.rad[0].checked)
    break;
    }
    alert ("Button " + Count + " is selected");
    {
    else (form.rad[1].checked)
    break;
    }
    alert ("Button" + Count + "is selected")
    }
    Code:
    for (Count = 0; Count < 3; Count++) {
      if (form.rad[Count].checked) {
        alert("Button " + Count + " is selected")
      }  
    }
    That should do the trick if you copy it exactly
  8. JamieClark's Avatar
    • Full Member
    • Location: Neath, Wales, United Kingdom
    • Posts: 114
    Re: IF statements URGENT!!! PLEASE HELP :)
    (Original post by Fried Sock)
    Code:
    for (Count = 0; Count < 3; Count++) {
      if (form.rad[Count].checked) {
        alert("Button " + Count + " is selected")
      }  
    }
    That should do the trick if you copy it exactly
    It'd probably be best to use the 'var' keyword in the initialization part of the 'for' loop to ensure that the scope of 'Count' is within the loop body only. Without it the variable has global scope and that can lead to all sorts of problems. I suppose it probably won't matter too much here but it's good practice nonetheless.

    Code:
    for (var Count = 0; Count < 3; Count++) {
      if (form.rad[Count].checked) {
        alert("Button " + Count + " is selected")
      }  
    }
    Last edited by JamieClark; 04-07-2012 at 09:10.
Sign in to Reply
Share this discussion:  
Useful resources
Article updates
Moderators

We have a brilliant team of more than 60 volunteers looking after discussions on The Student Room, helping to make it a fun, safe and useful place to hang out.

Reputation gems:
The Reputation gems seen here indicate how well reputed the user is, red gem indicate negative reputation and green indicates a good rep.
Post rating score:
These scores show if a post has been positively or negatively rated by our members.