Click to See Complete Forum and Search --> : Script Help
AJacobs633
01-17-2003, 10:59 AM
I am having trouble with this script, can y'all help me out here?
Here's the script:
<script langauge="javascript">
<!-- Hide
function grandtotal()
{
var reg=document.form1.RegType.options[document.form1.RegType.selectedIndex].value;
var tbl=document.form1.DTables.options[document.form1.DTables.selectedIndex].value;
var ttl=document.form1.Total;
if (tbl=="null" && reg=="null")
{ttl.value="0"};
else if (tbl == "null" && reg=="0")
{ttl.value="0"};
else if (tbl == "null" && reg=="80")
{ttl.value="80"};
else if (tbl == "null" && reg=="95")
{ttl.value="95"};
else if (tbl == "0" && reg=="null")
{ttl.value="0"};
else if (tbl == "0" && reg=="80")
{ttl.value="80"};
else if (tbl == "0" && reg=="95")
{ttl.value="95"};
else if (tbl == "100" reg=="null")
{ttl.value="100"};
else if (tbl == "100" && reg=="80")
{ttl.value="180"};
else if (tbl == "100" && reg=="95")
{ttl.value="195"};
else if (tbl=="150" && reg=="null")
{ttl.value="150"};
else if (tbl == "150" && reg=="80")
{ttl.value="230"};
else if (tbl == "150" && reg=="95")
{ttl.value="245"};
}
// End Hiding -->
</script>
And here are the relevent form items within the page:
<select name="RegType" onChange="grandtotal()">
<option value="null" selected>-- SELECT --</option>
<option value="95">Regular ($95)</option>
<option value="80">Early ($80)</option>
</select>
<SELECT name="DTables" onChange="grandtotal()">
<option value="null" selected>- SELECT -</option>
<option value="0">No</option>
<option value="150">Yes (for profit $150)</option>
<option value="100">Yes (nonprofit $100)</option>
</SELECT>
<INPUT type"text" size="10" name="Total">
Ok, thanks in advance...
mawood
01-17-2003, 11:27 AM
<html>
<head>
<title>Untitled</title>
<script langauge="javascript">
<!-- Hide
function grandtotal()
{
var reg=document.form1.RegType.options[document.form1.RegType.selectedIndex].value;
var tbl=document.form1.DTables.options[document.form1.DTables.selectedIndex].value;
var ttl=document.form1.Total;
if (tbl==null && reg==null)
{ttl.value="0"};
else if (tbl == null && reg=="0")
{ttl.value="0"};
else if (tbl == null && reg=="80")
{ttl.value="80"};
else if (tbl == null && reg=="95")
{ttl.value="95"};
else if (tbl == "0" && reg==null)
{ttl.value="0"};
else if (tbl == "0" && reg=="80")
{ttl.value="80"};
else if (tbl == "0" && reg=="95")
{ttl.value="95"};
else if (tbl == "100" && reg==null)
{ttl.value="100"};
else if (tbl == "100" && reg=="80")
{ttl.value="180"};
else if (tbl == "100" && reg=="95")
{ttl.value="195"};
else if (tbl=="150" && reg==null)
{ttl.value="150"};
else if (tbl == "150" && reg=="80")
{ttl.value="230"};
else if (tbl == "150" && reg=="95")
{ttl.value="245"};
}
// End Hiding -->
</script>
</head>
<body>
<form action="" name="form1" id="form1">
<select name="RegType" onChange="grandtotal()">
<option value="null" selected>-- SELECT --</option>
<option value="95">Regular ($95)</option>
<option value="80">Early ($80)</option>
</select>
<SELECT name="DTables" onChange="grandtotal()">
<option value="null" selected>- SELECT -</option>
<option value="0">No</option>
<option value="150">Yes (for profit $150)</option>
<option value="100">Yes (nonprofit $100)</option>
</SELECT>
<INPUT type"text" size="10" name="Total">
</form>
</body>
</html>
AJacobs633
01-17-2003, 11:37 AM
The only changes were to remove the quotes from around null? I still can't get it to work. Could it be because the script is not in the head, but in the body?
Webskater
01-17-2003, 11:40 AM
What is the problem?
Charles
01-17-2003, 11:40 AM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Form Example</title>
<script type="text/javascript">
<!--
function grandTotal () {document.form1.Total = Number(document.form1.DTables.options[document.form1.DTables.selectedIndex].value) + Number(document.form1.RegType.options[document.form1.RegType.selectedIndex].value)}
// -->
</script>
<div>
<form action="" name="form1" id="form1">
<select name="RegType" onChange="grandtotal()">
<option value="null" selected>-- SELECT --</option>
<option value="95">Regular ($95)</option>
<option value="80">Early ($80)</option>
</select>
<select name="DTables" onChange="grandTotal()">
<option value="null" selected>- SELECT -</option>
<option value="0">No</option>
<option value="150">Yes (for profit $150)</option>
<option value="100">Yes (nonprofit $100)</option>
</select>
<script type="text/javascript">
<!--
document.write('<input type"text" size="10" name="Total">');
// -->
</script>
<input type="submit">
</div>
</form>
mawood
01-17-2003, 11:54 AM
Ok, syntax is fixed, sorry:
<html>
<head>
<title>Untitled</title>
<script langauge="javascript">
<!-- Hide
function grandtotal()
{
var reg=document.form1.RegType.options[document.form1.RegType.selectedIndex].value;
var tbl=document.form1.DTables.options[document.form1.DTables.selectedIndex].value;
var ttl=document.form1.Total;
if (tbl==null && reg==null) {
ttl.value="0"
} else if (tbl == null && reg=="0") {
ttl.value="0"
} else if (tbl == null && reg=="80") {
ttl.value="80"
} else if (tbl == null && reg=="95") {
ttl.value="95"
} else if (tbl == "0" && reg==null) {
ttl.value="0"
} else if (tbl == "0" && reg=="80") {
ttl.value="80"
} else if (tbl == "0" && reg=="95") {
ttl.value="95"
} else if (tbl == "100" && reg==null) {
ttl.value="100"
} else if (tbl == "100" && reg=="80") {
ttl.value="180"
} else if (tbl == "100" && reg=="95") {
ttl.value="195"
} else if (tbl=="150" && reg==null) {
ttl.value="150"
} else if (tbl == "150" && reg=="80") {
ttl.value="230"
} else if (tbl == "150" && reg=="95") {
ttl.value="245"
}
}
// End Hiding -->
</script>
</head>
<body>
<form action="" name="form1" id="form1">
<select name="RegType" onChange="grandtotal()">
<option value="null" selected>-- SELECT --</option>
<option value="95">Regular ($95)</option>
<option value="80">Early ($80)</option>
</select>
<SELECT name="DTables" onChange="grandtotal()">
<option value="null" selected>- SELECT -</option>
<option value="0">No</option>
<option value="150">Yes (for profit $150)</option>
<option value="100">Yes (nonprofit $100)</option>
</SELECT>
<INPUT type"text" size="10" name="Total">
</form>
</body>
</html>
Charles
01-17-2003, 12:00 PM
You're still making it a great deal more complicated than you need to. What's with all the if statements just to add two numbers?
mawood
01-17-2003, 12:10 PM
I understand that. I was just fixing the code. Maybe there is more to the code than was shown. Sure it would be easier to just add them.
Webskater
01-17-2003, 12:43 PM
Will javascript add null values? Why not set the first value of the first select box to 0 instead of null?
This is a purely subjective comment based on a vague memory rather than anything concrete but I know when writing complex validation scripts that, sometimes, complex if ... else if statements fail if they do not have an 'else' statement at the end.
I have to say I'd love to know what the problem is - rather than just being told 'it does not work'.
Charles
01-17-2003, 12:55 PM
JavaScript will take null to be zero. I think that Jacobs633 is using that troublesome string of if statements to keep the field values from concatenating. He or she doesn't know about using Number as a cast. JavaScript is a lot like Perl in that if there has to be an easier way then there is.
AJacobs633
01-17-2003, 12:55 PM
Hummm, I can get the script to work within a blank document, but when I place it in my page it can't find the RegType select item. Grrr... I'll attach the code for the page so you all can look at it. Ok, thanks in advance.
AJacobs633
01-17-2003, 01:05 PM
You are correct, Charles, I really know very little about Javascripting. I figured there was a way to just add the numbers, but didn't it. I did know about if/else statements, so that's what I used. Now, incedentily, the script you posted earlier I tried, and it just gave me erors.
Oh, by the way, I am a blind man. :-)
Charles
01-17-2003, 01:15 PM
And I, by the way, am a lazy man. I took mawood's method as working and I didn't subject my example to testing. Try this one:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Form Example</title>
<script type="text/javascript">
<!--
function grandTotal () {document.form1.Total.value = Number(document.form1.DTables.options[document.form1.DTables.selectedIndex].value) + Number(document.form1.RegType.options[document.form1.RegType.selectedIndex].value)}
// -->
</script>
<div>
<form action="" name="form1" id="form1">
<select name="RegType" onChange="grandTotal()">
<option value="0" selected>-- SELECT --</option>
<option value="95">Regular ($95)</option>
<option value="80">Early ($80)</option>
</select>
<select name="DTables" onChange="grandTotal()">
<option value="0" selected>- SELECT -</option>
<option value="0">No</option>
<option value="150">Yes (for profit $150)</option>
<option value="100">Yes (nonprofit $100)</option>
</select>
<script type="text/javascript">
<!--
document.write('<input type"text" size="10" name="Total">');
// -->
</script>
<input type="submit">
</div>
</form>
AJacobs633
01-17-2003, 02:19 PM
Thank you Charles, that worked, and I'll remember the format for adding things. :-)
AJacobs633
01-17-2003, 02:48 PM
Ok, I think Dreamweaver is making my tables to complex and redundent. When I load the page and try it out, it can't find the Total imput box. and then there is an unexpected object eror. Again the text of the page, with Charles' script is attached.