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 | |
-
Re: IF statements URGENT!!! PLEASE HELP :)Fixed by adding [ CODE ] and [ /CODE ] tags.(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"); } }
There's the unclosed brackets that's already been mentioned.
Also else doesn't take a parameter so you either want
orCode:if (something) { action; } else { other_action; }
the final else statement is taken if none of the preceding actions are taken.Code:if (something) { action; } else if (something_else) { other_action; } -
Re: IF statements URGENT!!! PLEASE HELP :)this was 1st attempt it worked but only for the first button iv posted a newer version since x(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.
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?(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? -
Re: IF statements URGENT!!! PLEASE HELP :)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.(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? -
Re: IF statements URGENT!!! PLEASE HELP :)oh. ... what do you recommend?(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. -
Re: IF statements URGENT!!! PLEASE HELP :)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.(Original post by Sparrow_Squire)
oh. ... what do you recommend? -
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")
} -
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")
}That should do the trick if you copy it exactlyCode:for (Count = 0; Count < 3; Count++) { if (form.rad[Count].checked) { alert("Button " + Count + " is selected") } } -
Re: IF statements URGENT!!! PLEASE HELP :)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.(Original post by Fried Sock)
That should do the trick if you copy it exactlyCode:for (Count = 0; Count < 3; Count++) { if (form.rad[Count].checked) { alert("Button " + Count + " is selected") } }
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.