Wrox Programmer Forums
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 8th, 2003, 12:45 PM
Registered User
 
Join Date: Aug 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default IF ELSE problem

I'm trying to write a script that will give a certain response based upon a numeric value that is generated from values entered into a form. the user enters two numeric values(V1 and V2) and clicks a button that tells you the percentage that V1 is of V2. if that percentage is less than say.....80, than a certain message is generated ELSE another answer is generated. i've been able to write the calculator part, but am having problems with the subsequent statement. my code is as follows....

<html>
<head><title>Trade Program</title></head>
<body><center><h1>Is Your Trade Legit???</h1></center>
<SCRIPT language=JavaScript>
<!--

function tom1() {
 a = document.form1.c.value;
 b = document.form1.d.value;
 c = a/b;
 d = c*100;
 document.form1.total2.value = d
 }
//-->
</SCRIPT>
<FORM name=form1>
<TABLE cellSpacing=1 cellPadding=1 border=1>
  <TBODY>
  <TR>
    <TD align=middle colSpan=3><B>Trade Calculator</B>
      </TD></TR>
    <TR><TD><INPUT size=5 name=c> is what percent of <INPUT size=5
name=d>?</TD>
    <TD>Answer: <INPUT size=5 name=total2> %</TD>
    <TD><INPUT onclick=tom1() type=button value=Calculate></TD></TR>
  <TR>
    <TD align=middle
colSpan=3><INPUT type=reset value=Reset></TD></TR></TBODY></TABLE></FORM>
<SCRIPT Language=JavaScript>

<!-- This part isn't working

if (document.form1.Calculate < 80) {
document.write("no")
}
else {
document.write("yes")
}
//-->
</SCRIPT>
</body>
</html>

any suggestions would be appreciated


 
Old August 8th, 2003, 03:36 PM
Authorized User
 
Join Date: Jun 2003
Posts: 83
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to natmaster Send a message via AIM to natmaster Send a message via MSN to natmaster
Default

Well there are actually several problems with your script. first of all, javascript recognizes only the name tag to reference your objects in html.

in other words change:
<INPUT onclick=tom1() type=button value=Calculate>
to:
<INPUT onclick="tom1();" type="button" value="Calculate" name="Calculate">

you might have noticed some other changes i made...it's a good idea to always put values in quotation marks, just a good html programming technique because some times html cannot pick up certain symbols unless they are in quotes so your code will be mangled.


also, you need to trigger the last event
so make the code at the bottom:
function dispMsg() {
if (document.form1.total2< 80) {
document.write("no")
}
else {
document.write("yes")
}
}
and move this code up to the top so it's defined before the html

and then to trigger it, change:
<INPUT size=5 name=total2>
to <INPUT size="5" name="total2" onChange="dispMsg();">

----------------------------
http://aeonofdarkness.com
 
Old August 8th, 2003, 03:52 PM
uit uit is offline
Authorized User
 
Join Date: Jul 2003
Posts: 36
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Which is more appropriate, single or double quotes within the HTML element names, ids, etc?

Example:
<INPUT onclick='tom1();' type='button' value='Calculate' name='Calculate'>
-or-
<INPUT onclick="tom1();" type="button" value="Calculate" name="Calculate">

I assume that whatever the answer, to remain consistant...

Thanks!
Anthony
 
Old August 8th, 2003, 07:32 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 101
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via AIM to Moharo
Default

it really doesn't matter but i think using double quotes is better...

the genuine genius
 
Old August 9th, 2003, 03:14 PM
Authorized User
 
Join Date: Jun 2003
Posts: 83
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to natmaster Send a message via AIM to natmaster Send a message via MSN to natmaster
Default

double quotes are usually better because the follow the logic of biggest grouping around the most......in some cases you will need to use quotes in your value, so you use the single quotes (smaller grouping symbol) to contain those.

example:
<a href="http://aeonofdarkness.com" onClick="Alert('hiya');">Aeon of Darkness</a>

----------------------------
http://aeonofdarkness.com









Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.