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 | |
|---|---|---|
-
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????
-
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> -
Re: IF statements URGENT!!! PLEASE HELP :)Try here.(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????
-
Re: IF statements URGENT!!! PLEASE HELP :)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> -
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. -
Re: IF statements URGENT!!! PLEASE HELP :)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).(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> -
Re: IF statements URGENT!!! PLEASE HELP :)You're missing a second "if" statement for rad_button2.(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> -
Re: IF statements URGENT!!! PLEASE HELP :)Ahhhh i see what you mean il hav a fiddle(Original post by Fried Sock)
If you look closely, you are not following this format at the moment.
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(Original post by ANARCHY__)
Would it be elseif?
im just gonna try alter it and see what i get cheers guys ill be in touch!
-
Re: IF statements URGENT!!! PLEASE HELP :)maybe i didnt see(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.
looks same to me...?
-
Re: IF statements URGENT!!! PLEASE HELP :)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. -
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
-
Re: IF statements URGENT!!! PLEASE HELP :)Are you missing those brackets highlighted?(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
-
Re: IF statements URGENT!!! PLEASE HELP :)i dont understand? do you mean i shouldn't have those 2 in the code?(Original post by ANARCHY__)
Are you missing those brackets highlighted? -
Re: IF statements URGENT!!! PLEASE HELP :)No no. You have those two brackets there but they're open. There's no closing bracket that I can see.(Original post by Sparrow_Squire)
i dont understand? do you mean i shouldn't have those 2 in the code? -
Re: IF statements URGENT!!! PLEASE HELP :)ahhhhh i didnt notice that :s it hasnt made any difference though? ive tried;(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.
(document.testform.getElementByI D("pCar")).checked{
(document.testform.getElementByI D("pCar").checked){
document.testform.getElementByID ("pCar").checked{
it hasnt changed anything? -
Re: IF statements URGENT!!! PLEASE HELP :)What is the actual error it throws out when you do this?(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? -
Re: IF statements URGENT!!! PLEASE HELP :)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.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>Last edited by mikeyd85; 03-07-2012 at 13:16. -
Re: IF statements URGENT!!! PLEASE HELP :)Why document.testform on one line and then form on the other? Surely this would be calling from a different form?(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
any ideas please????