Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old August 8th, 2003, 12:45 PM
Registered User
 
Join Date: Aug 2003
Location: , , .
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


Reply With Quote
  #2 (permalink)  
Old August 8th, 2003, 03:36 PM
Authorized User
 
Join Date: Jun 2003
Location: , , USA.
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
Reply With Quote
  #3 (permalink)  
Old August 8th, 2003, 03:52 PM
uit uit is offline
Authorized User
 
Join Date: Jul 2003
Location: , , .
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
Reply With Quote
  #4 (permalink)  
Old August 8th, 2003, 07:32 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
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
Reply With Quote
  #5 (permalink)  
Old August 9th, 2003, 03:14 PM
Authorized User
 
Join Date: Jun 2003
Location: , , USA.
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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off




All times are GMT -4. The time now is 03:40 PM.


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