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
Sign in to Reply
  1. Sparrow_Squire's Avatar
    • Junior Member
    • Location: Huddersfield
    • Posts: 58
    IF statements URGENT!!! PLEASE HELP :)
    basically im trying to code a radio form with JS this is what ive got so far;

    <script type="text/javascript">
    function testButton (form){
    for (Count = 0; Count < 3; Count++) {
    if (form.rad[0].checked)
    break;
    }
    alert ("Button " + Count + " is selected");
    }
    </script>


    it works for rad[0] but wen i add;
    else
    {
    (form.rad[1].checked)
    break;
    }
    alert ("blah");
    }

    it kills all my JS any ideas please????
  2. Fallen's Avatar
    • Overlord in Training
    Re: IF statements URGENT!!! PLEASE HELP :)
    What do you think this line is supposed to do, because it isn't valid syntax...

    Code:
    (form.rad[1].checked)
  3. Sparrow_Squire's Avatar
    • Junior Member
    • Location: Huddersfield
    • Posts: 58
    Re: IF statements URGENT!!! PLEASE HELP :)
    oh? it works tho?
    suposed to be if rad[1] is checked then this message box appears. heres the html (prob give you a better idea);

    <FORM NAME="testform">
    <INPUT TYPE="radio" NAME="rad" Value="rad_button1" onClick=0>Petrol<BR>
    <INPUT TYPE="radio" NAME="rad" Value="rad_button2" onClick=0>Diesel<BR>
    <INPUT TYPE="radio" NAME="rad" Value="rad_button3" onClick=0>Any<BR>
    <INPUT TYPE="button" NAME="button" Value="Click" onClick="testButton(this.form)"> <BR>
    </FORM>
  4. ANARCHY__'s Avatar
    • Overlord in Training
    • Posts: 2,556
    Re: IF statements URGENT!!! PLEASE HELP :)
    (Original post by Sparrow_Squire)
    basically im trying to code a radio form with JS this is what ive got so far;

    <script type="text/javascript">
    function testButton (form){
    for (Count = 0; Count < 3; Count++) {
    if (form.rad[0].checked)
    break;
    }
    alert ("Button " + Count + " is selected");
    }
    </script>


    it works for rad[0] but wen i add;
    else
    {
    (form.rad[1].checked)
    break;
    }
    alert ("blah");
    }

    it kills all my JS any ideas please????
    Try here.
  5. Sparrow_Squire's Avatar
    • Junior Member
    • Location: Huddersfield
    • Posts: 58
    Re: IF statements URGENT!!! PLEASE HELP :)
    (Original post by ANARCHY__)
    Try here.
    thanks but that suggests the following sort of thing and whatever im doing wrong its not solved like this dont want the answer just a bit of guidence where im going wrong cos i just cant see it;

    <script type="text/javascript">
    function testButton (form){
    for (Count = 0; Count < 3; Count++) {
    if (form.getElementById("rad_button 1").checked
    {
    alert("gah");
    }
    else
    {
    (form.getElementById("rad_button 2").checked

    }
    alert ("blah");
    }

    </script>
  6. Fried Sock's Avatar
    • Full Member
    • Location: Preston
    • Posts: 97
    Re: IF statements URGENT!!! PLEASE HELP :)
    if statements follow the form

    if (boolean) {
    //stuff
    } else {
    //stuff
    }

    or you can extend this format with an else if branch (I believe this will help you)

    if (boolean1) {
    //stuff
    } else if (boolean2) {
    //stuff
    } else {
    /stuff
    }

    If you look closely, you are not following this format at the moment.
  7. ANARCHY__'s Avatar
    • Overlord in Training
    • Posts: 2,556
    Re: IF statements URGENT!!! PLEASE HELP :)
    (Original post by Sparrow_Squire)
    thanks but that suggests the following sort of thing and whatever im doing wrong its not solved like this dont want the answer just a bit of guidence where im going wrong cos i just cant see it;

    <script type="text/javascript">
    function testButton (form){
    for (Count = 0; Count < 3; Count++) {
    if (form.getElementById("rad_button 1").checked
    {
    alert("gah");
    }
    else
    {
    (form.getElementById("rad_button 2").checked

    }
    alert ("blah");
    }

    </script>
    Would it be elseif? Maybe I'm reading this wrong but it seems like your code assumes radio button 2 is checked which may not be the case (unless you've focussed on radio button 1).
  8. Vulpes's Avatar
    • PS Helper
    • TSR Demigod
    Re: IF statements URGENT!!! PLEASE HELP :)
    (Original post by Sparrow_Squire)
    thanks but that suggests the following sort of thing and whatever im doing wrong its not solved like this dont want the answer just a bit of guidence where im going wrong cos i just cant see it;

    <script type="text/javascript">
    function testButton (form){
    for (Count = 0; Count < 3; Count++) {
    if (form.getElementById("rad_button 1").checked
    {
    alert("gah");
    }
    else
    {
    (form.getElementById("rad_button 2").checked

    }
    alert ("blah");
    }

    </script>
    You're missing a second "if" statement for rad_button2.
  9. Sparrow_Squire's Avatar
    • Junior Member
    • Location: Huddersfield
    • Posts: 58
    Re: IF statements URGENT!!! PLEASE HELP :)
    (Original post by Fried Sock)
    If you look closely, you are not following this format at the moment.
    Ahhhh i see what you mean il hav a fiddle


    (Original post by ANARCHY__)
    Would it be elseif?
    i understood it as if its a 2 parter its if..else.. more parts its if... else if ... else if... else. i have got more parts but im getting this functional first

    im just gonna try alter it and see what i get cheers guys ill be in touch!
  10. Sparrow_Squire's Avatar
    • Junior Member
    • Location: Huddersfield
    • Posts: 58
    Re: IF statements URGENT!!! PLEASE HELP :)
    (Original post by Fried Sock)
    if statements follow the form

    if (boolean) {
    //stuff
    } else {
    //stuff
    }

    or you can extend this format with an else if branch (I believe this will help you)

    if (boolean1) {
    //stuff
    } else if (boolean2) {
    //stuff
    } else {
    /stuff
    }

    If you look closely, you are not following this format at the moment.
    maybe i didnt see looks same to me...?
  11. Fried Sock's Avatar
    • Full Member
    • Location: Preston
    • Posts: 97
    Re: IF statements URGENT!!! PLEASE HELP :)
    (Original post by Sparrow_Squire)
    maybe i didnt see looks same to me...?
    Ok, at the moment you are missing a closing bracket at the top of the if statement.

    if (form.getElementById("rad_button 1").checked) {
    alert("gah")
    } else if (form.getElementById("rad_button 2").checked) {
    break;
    }

    At the moment you just have form.getElementById("rad_button 2").checked as a one line statement, which isn't doing anything since it is just an attribute.
    Also, if you are not planning in having any code in the 2nd branch of the statement then I would suggest notting it and removing the 3rd branch.
  12. Sparrow_Squire's Avatar
    • Junior Member
    • Location: Huddersfield
    • Posts: 58
    Re: IF statements URGENT!!! PLEASE HELP :)
    this is current JS

    function testButton (form){
    if
    (document.testform.getElementByI D("pCar").checked
    {
    document.write("gah");
    }
    else
    (form.getElementByID("dCar").che cked
    {
    document.write("blah");
    }
    }

    HTML

    <FORM NAME="testform">
    <INPUT TYPE="radio" ID="pCar" NAME="rad" Value="rad_button1" onClick=0>Petrol<BR>
    <INPUT TYPE="radio" ID="dCar" NAME="rad" Value="rad_button2" onClick=0>Diesel<BR>
    <INPUT TYPE="radio" NAME="rad" Value="rad_button3" onClick=0>Any<BR>
    <INPUT TYPE="button" NAME="button" Value="Click" onClick="testButton(this.form)"> <BR>
    </FORM>



    REALLY REALLY stuck tried everything i can think of please help only 3 days left
  13. ANARCHY__'s Avatar
    • Overlord in Training
    • Posts: 2,556
    Re: IF statements URGENT!!! PLEASE HELP :)
    (Original post by Sparrow_Squire)
    this is current JS

    function testButton (form){
    if
    (document.testform.getElementByID ("pCar").checked
    {
    document.write("gah");
    }
    else
    (form.getElementByID("dCar").chec ked
    {
    document.write("blah");
    }
    }

    HTML

    <FORM NAME="testform">
    <INPUT TYPE="radio" ID="pCar" NAME="rad" Value="rad_button1" onClick=0>Petrol<BR>
    <INPUT TYPE="radio" ID="dCar" NAME="rad" Value="rad_button2" onClick=0>Diesel<BR>
    <INPUT TYPE="radio" NAME="rad" Value="rad_button3" onClick=0>Any<BR>
    <INPUT TYPE="button" NAME="button" Value="Click" onClick="testButton(this.form)"> <BR>
    </FORM>



    REALLY REALLY stuck tried everything i can think of please help only 3 days left
    Are you missing those brackets highlighted?
  14. Sparrow_Squire's Avatar
    • Junior Member
    • Location: Huddersfield
    • Posts: 58
    Re: IF statements URGENT!!! PLEASE HELP :)
    (Original post by ANARCHY__)
    Are you missing those brackets highlighted?
    i dont understand? do you mean i shouldn't have those 2 in the code?
  15. ANARCHY__'s Avatar
    • Overlord in Training
    • Posts: 2,556
    Re: IF statements URGENT!!! PLEASE HELP :)
    (Original post by Sparrow_Squire)
    i dont understand? do you mean i shouldn't have those 2 in the code?
    No no. You have those two brackets there but they're open. There's no closing bracket that I can see.
  16. Sparrow_Squire's Avatar
    • Junior Member
    • Location: Huddersfield
    • Posts: 58
    Re: IF statements URGENT!!! PLEASE HELP :)
    (Original post by ANARCHY__)
    No no. You have those two brackets there but they're open. There's no closing bracket that I can see.
    ahhhhh i didnt notice that :s it hasnt made any difference though? ive tried;
    (document.testform.getElementByI D("pCar")).checked{

    (document.testform.getElementByI D("pCar").checked){

    document.testform.getElementByID ("pCar").checked{

    it hasnt changed anything?
  17. ANARCHY__'s Avatar
    • Overlord in Training
    • Posts: 2,556
    Re: IF statements URGENT!!! PLEASE HELP :)
    (Original post by Sparrow_Squire)
    ahhhhh i didnt notice that :s it hasnt made any difference though? ive tried;
    (document.testform.getElementByI D("pCar")).checked{

    (document.testform.getElementByI D("pCar").checked){

    document.testform.getElementByID ("pCar").checked{

    it hasnt changed anything?
    What is the actual error it throws out when you do this?
  18. Sparrow_Squire's Avatar
    • Junior Member
    • Location: Huddersfield
    • Posts: 58
    Re: IF statements URGENT!!! PLEASE HELP :)
    it just doesnt produce any message or alert when i click the button ive tried everything i can think of im really running on empty with this now
  19. mikeyd85's Avatar
    • TSR Royalty
    • Location: Leeds
    • Posts: 18,909
    Re: IF statements URGENT!!! PLEASE HELP :)
    Code:
    <script type="text/javascript">
    function testButton (form){
    for (Count = 0; Count < 3; Count++) {
    if (form.rad[0].checked)
    break;
    }
    alert ("Button " + Count + " is selected");
    }
    
    else
    
    {
    alert ("blah");
    }
    </script>
    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.
    Last edited by mikeyd85; 03-07-2012 at 13:16.
  20. ANARCHY__'s Avatar
    • Overlord in Training
    • Posts: 2,556
    Re: IF statements URGENT!!! PLEASE HELP :)
    (Original post by Sparrow_Squire)
    it just doesnt produce any message or alert when i click the button ive tried everything i can think of im really running on empty with this now
    Why document.testform on one line and then form on the other? Surely this would be calling from a different form?
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.