looking for a short IF statement example.

G

Guest

Please could someone tell me how i insert a IF statement within a .JS file

i have a script file called myscript1.js

when it is called it calculates a sum and depending on the answer it will
print certain words.

i.e.


function Gettotal()
{

var form = document.forms.selecteddata;

if eval(form.Avocado.value)>0
{
parent.orderlist.document.getElementById('b_Avocado').innerHTML =
adddecimal(eval(form.Avocado.value*cAvocado));
}

}





if you are not sure what i mean, please ask for another example of what i am
trying to do.

i am trying to have an IF statment in side a script function.

but the script does not run, if i take out the IF staement the rest of the
script runs ok, but i need to have stuff printed in parts of the window if
the sum is a certain number.

Thank you
 
S

Steve Easton

Look for an If Then Else Statement.


--
Steve Easton
Microsoft MVP FrontPage
95isalive
This site is best viewed..................
...............................with a computer
 
G

Guest

i have done and inserted it in the .js script file and it does not work.

is like this..

and it does not run the IF statement, if i take out the If statement the
script works ok, but it needs this if statement to finish of what the script
was made for.
should it be a seperate .JS file and called within this Gettotal function ?


function Gettotal()
{

var form = document.forms.orders;

ctotal=eval(form.Avocado.value*cAvocado);
ctotal=ctotal+eval(form.Butter_Squash.value*cButter_Squash);
....
...
...

if (eval(form.Avocado.value) >0)
{
parent.orderlistcell.document.getElementById('b_Avocado').innerHTML =
adddecimal(eval(form.Avocado.value*cAvocado));
}
else
{
parent.orderlistcell.document.getElementById('b_Avocado').innerHTML = "";
}



form.total.value = "£"+adddecimal(ctotal/100)+"p";

parent.totalcostcell.document.getElementById('theTotal').innerHTML =
adddecimal(ctotal/100);



}
 
K

Kevin Spencer

i am trying to have an IF statment in side a script function.

Not exactly. I'm not sure exactly WHAT you're trying to do, but what you
think you're trying to do is a HOW, not a WHAT. I believe your HOW is in
error, so if you could tell us in detail what you want your function to do,
I can supply a solution. Note that "when it is called it calculates a sum"
is not specific enough. A sum, for example, is the product of adding numbers
together. "depending on the answer it will print certain words" is not
specific enough. Since your code is in error, I can' tell WHAT words it
wants to print. An example of a good explanation of your business
requirement might be something like the following:

"I have a text field which only accepts numbers, and will not validate until
there is a value in the text field. The name of the text field is 'Avocado.'
I want to use the number input in that field. When the text field loses
focus (not when the form is submitted, of course), I want to display the
number in a div. The div is on the same page, and has an id of 'b_Avocado'"

This is probably not exactly what you are tryiing to do, but notice that it
leaves little room for the imagination. I could write a function that
performs this in a few minutes, and would be glad to tell you how to, if you
could give just as good an explanation of your requirement.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Sometimes you eat the elephant.
Sometimes the elephant eats you.
 
G

Guest

i would like to know how to insert an IF statement within a function. below
is what i believe was right. but i am wrong as the IF statement does not
run.
if i take out the ELSE part of the statement i am half way there, but i need
the ELSE part too.

i know that there is an easier way to show what the total is, by just
displaying the total, but this would not show me how the if statement works.
thanks

this is not my .JS file it is an example that would hopefully help me
understand how to use the IF and ELSE statements within a .JS file.

lets say my .JS file takes the form.

function nameoffunction() {
a=1
b=2
c=3
total=eval(a+b+c)

if (eval(total)) =1
{
document.write("the total is 1")
}
else
{
document.write("the total was not 1")
}

if (eval(total)) =1
{
document.write("the total is 2")
}
else
{
document.write("the total was not 2")
}

if (eval(total)) =1
{
document.write("the total is 3")
}
else
{
document.write("the total was not 3")
}

}
 
K

Kevin Spencer

Well, I gave it my best shot. Good luck!

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Sometimes you eat the elephant.
Sometimes the elephant eats you.
 
G

Guest

?

Kevin Spencer said:
Well, I gave it my best shot. Good luck!

--
HTH,

Kevin Spencer
Microsoft MVP
.Net Developer
Sometimes you eat the elephant.
Sometimes the elephant eats you.
 
S

Steve Easton

For the subsequent If statements after the first IF you should use
Else If


--
Steve Easton
Microsoft MVP FrontPage
95isalive
This site is best viewed..................
...............................with a computer
 
G

Guest

looking at my example i realiase it was not correct.

this is a better example of how i am wanting to use the If and ELSE IF
statements
but the ELSE IF part does not work. the first IF statement work the ELSE IF
does not.
also if i have the ELSE IF part replaced with another IF line i.e

i know this is not formed right but you get the jist.
if this=1 then print 'this =1'
if this=0 then print 'this is not 1'
....
....
....

without the use of the ELSE IF it still does not work.

below is the example of what i am attempting to do.


the .js file that is called in one of my iframes.

function nameoffunction() {
a=1;
b=2;
c=3;



if (eval(a)=1) {
document.write("a=1");
}
else
{
document.write("a does not =1");
}



if (eval(b)=1) {
document.write("b=2");
}
else
{
document.write("b does not=2");
}



if (eval(c)=1) {
document.write("c=3");
}
else
{
document.write("c does not=3");
}



}






i have looked on the internet for examples of IF ... ELSE IF statements.

they all take the form...

IF (morethanzero=1) {
document.write("more than zero");
}
ELSE {
document.write("not more than zero");
}


so why do all of mine not work
 
S

Steve Easton

Why are you using eval??

have you tried just

If (a=1) {
document.write("a=1");
}
Else
{
document.write("a does not =1");
}
If (b=1) {
document.write("b=2");
}
Else
{
document.write("b does not=2");
}

Also remember that javascript is CaSe sensative.

--
Steve Easton
Microsoft MVP FrontPage
95isalive
This site is best viewed............
........................with a computer
 
G

Guest

If not IF or iF or if
Else not ELSE or ElSe

correct ?

ok but still the If and Else statements does not work.

also i changed the all of the If and Else statements for a single
alert("a")
this works and the alert box shows, i have also added this alert("a") like
so

a=1;
If (a=1);
{
alert("a")
}

but the alert box does not show

i have also tried adding var a=1 in place of a=1 to assign the variable.

but this also does not work.
the alert box does not show.

this has got to be something simple, i think we have gone through all but
the correct way to do this.

again, just to make sure you know where these If Else statements are
going.
i have a file called myscripts.js this file starts...

function myscripts() {
...
...
the rest of the script works ok.
...
until it gets to the bottom before the script is terminated with the final }

where it has all that we have listed above. (the If and Else parts of the
script)


where are we going wrong.

Jason
 
J

Jens Peter Karlsen[FP MVP]

There are no ELSE IF in your code example. It would be something like
this.

if (number > 0)
{
alert("Number is a positive integer");
}
else if (number < 0)
{
alert("Number is a negative integer");
}
else
{
alert("Number is 0");
}

You can have multible else if statements before the finishing else
statement.

Regards Jens Peter Karlsen. Microsoft MVP - Frontpage.
-----Original Message-----
From: cheers [mailto:cheers]
Posted At: 16. maj 2005 20:57
Posted To: microsoft.public.frontpage.programming
Conversation: looking for a short IF statement example.
Subject: Re: looking for a short IF statement example.


looking at my example i realiase it was not correct.

this is a better example of how i am wanting to use the If
and ELSE IF statements but the ELSE IF part does not work.
the first IF statement work the ELSE IF does not.
also if i have the ELSE IF part replaced with another IF line i.e

i know this is not formed right but you get the jist.
if this=1 then print 'this =1'
if this=0 then print 'this is not 1'
...
...
...

without the use of the ELSE IF it still does not work.

below is the example of what i am attempting to do.


the .js file that is called in one of my iframes.

function nameoffunction() {
a=1;
b=2;
c=3;



if (eval(a)=1) {
document.write("a=1");
}
else
{
document.write("a does not =1");
}



if (eval(b)=1) {
document.write("b=2");
}
else
{
document.write("b does not=2");
}



if (eval(c)=1) {
document.write("c=3");
}
else
{
document.write("c does not=3");
}



}






i have looked on the internet for examples of IF ... ELSE
IF statements.

they all take the form...

IF (morethanzero=1) {
document.write("more than zero");
}
ELSE {
document.write("not more than zero");
}


so why do all of mine not work






Steve Easton said:
For the subsequent If statements after the first IF you should use
Else If


--
Steve Easton
Microsoft MVP FrontPage
95isalive
This site is best viewed..................
 
G

Guest

yes that work when the alert box function is used. but not when used like
this.

i.e.

function Gettotal()
{
var form = document.forms.favororders;
form.total.value = "£"+adddecimal(ctotal/100)+"p";
parent.totalcostcell.document.getElementById('theTotal').innerHTML =
adddecimal(ctotal/100);
if (form.Avocado.value==0) {
alert("0");
}
else {
alert("not 0")
}

if (form.Avocado.value==1) {
alert("1");
}
else {
alert("not 1")
}

if (form.Avocado.value==2) {
alert("2");
}
else {
alert("not 2")
}
// all the above lines work when all of the below lines if..{..}
else { ... } are not in the script.
// but when the below lines are present in the script the above 2 lines
// ( form.total.value and parent.totalcostcell....) do nothing, and
the values are not changed. in the form.total.. and the
parent.totalcostcell..

if (form.Butter_Squash.value==0) {
parent.orderlistcell.document.getElementById('b_Butter_Squash').innerHTML =
"";
}
else {
parent.orderlistcell.document.getElementById('b_Butter_Squash').innerHTML =
adddecimal(form.Butter_Squash.value*cButter_Squash);
}

if (form.Cauliflower.value==0) {
parent.orderlistcell.document.getElementById('b_Cauliflower').innerHTML =
"";
}
else {
parent.orderlistcell.document.getElementById('b_Cauliflower').innerHTML =
adddecimal(form.Cauliflower.value*cCauliflower);
}

}
 
S

Steve Easton

Remove the underscores from the variable names, and use ButterSquash instead of Butter_Squash.

javascript variables need to be letters and numbers, and always start with a letter.

--
Steve Easton
Microsoft MVP FrontPage
95isalive
This site is best viewed............
........................with a computer
 
G

Guest

SORTED

// this works
if (form.Avocado.value==2) {
alert(form.Avocado.value);
}
else {
alert(form.Avocado.value)
}


// this does not
if (form.Avocado.value==0) {
parent.orderlistcell.document.getElementById('bAvocado').innerHTML = "";
}
else {
parent.orderlistcell.document.getElementById('bAvocado').innerHTML =
adddecimal(form.Avocado.value*cAvocado);
}


it was something very simple !!!

" not " ( the first parent.... line ) i don't even know how to get
this charactor, but i did. !
also adddecimal(form.Avocado.value*cAvocado);
should have been adddecimal((form.Avocado.value)*cAvocado);


THANK YOU to ALL that have helped anyway.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top