Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > BOOK: Beginning JavaScript
|
BOOK: Beginning JavaScript
This is the forum to discuss the Wrox book Beginning JavaScript by Paul Wilton; ISBN: 9780764544057
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning JavaScript 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 September 16th, 2005, 07:03 PM
Registered User
 
Join Date: May 2005
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Question 2 From Chapter 6 Question

I cannot get the code to work for question two at the end of chapter 6. Does anyone know if this does indeed work?

 
Old November 22nd, 2005, 05:09 PM
Registered User
 
Join Date: Nov 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Yes - it does work (I suppose you are talking about ch6_q2.htm)
 
Old April 16th, 2007, 03:15 PM
Registered User
 
Join Date: Apr 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I copied out the example, it more or less works except the processor isn't added to the total. Can anybody see what I have missed?

<html>
<head>

<script language = JavaScript>
var CompItems = new Array();

CompItems[100] = 1000;
CompItems[101] = 1250;
CompItems[102] = 1500;

CompItems[200] = 35;
CompItems[201] = 65;
CompItems[202] = 95;

CompItems[300] = 50;
CompItems[301] = 75;
CompItems[302] = 100;

CompItems[400] = 10;
CompItems[401] = 15;
CompItems[402] = 25;

function updateOrderDetails()
{
    var total = 0;
    var orderDetails = "";
    var formElement;

    formElement =
        document.form1.cboProcessor[document.form1.cboProcessor.selectedIndex];
    total = parseFloat (CompItems[formElement.value]);
    orderDetails = "Processor : " + formElement.text;
    orderDetails = orderDetails + " $" + CompItems[formElement.value] + "\n";

    formElement =
        document.form1.cboHardDrive[document.form1.cboHardDrive.selectedIndex];
    total = parseFloat (CompItems[formElement.value]);
    orderDetails = "Hard Drive : " + formElement.text;
    orderDetails = orderDetails + " $" + CompItems[formElement.value] + "\n";

    formElement = document.form1.chkCDROM
    if (formElement.checked == true)
    {
        orderDetails = orderDetails + "CD-ROM : $" +
          CompItems[formElement.value] + "\n";
        total = total + parseFloat(CompItems[formElement.value]);
    }

    formElement = document.form1.chkDVD
    if (formElement.checked == true)
    {
        orderDetails = orderDetails + "DVD-ROM : $" +
          CompItems[formElement.value] + "\n";
        total = total + parseFloat(CompItems[formElement.value]);
    }

    formElement = document.form1.chkScanner
    if (formElement.checked == true)
    {
        orderDetails = orderDetails + "Scanner : $" +
          CompItems[formElement.value] + "\n";
        total = total + parseFloat(CompItems[formElement.value]);
    }

    formElement = document.form1.radCase
    if (formElement[0].checked == true)
    {
        orderDetails = orderDetails + "Desktop Case : $" +
          CompItems[formElement[0].value] + "\n";
        total = total + parseFloat(CompItems[formElement[0].value]);
    }
    else if (formElement[1].checked == true)
    {
        orderDetails = orderDetails + "Mini Tower Case : $" +
          CompItems[formElement[1].value] + "\n";
        total = total + parseFloat(CompItems[formElement[1].value]);
    }
    else
    {
        orderDetails = orderDetails + "Full Tower Case : $" +
          CompItems[formElement[2].value] + "\n";
        total = total + parseFloat(CompItems[formElement[2].value]);
    }

    orderDetails = orderDetails + "\n\nTotal Order Cost is $" + total;

    document.form1.txtOrder.value = orderDetails;
    }

</script>
</head>

<body>

<form name=form1>
<table>
<TR>
<TD width = 300>
Processor
<br>
<select name = cboProcessor >
    <option value = 100 >MegaPro 1ghz</option>
    <option value = 101 >MegaPro 1.2ghz</option>
    <option value = 102 >MegaPro 1.5ghz</option>

</select>
<br><br>
Hard drive
<br>
<select name = cboHardDrive >
    <option value = 200>30gb</option>
    <option value = 201>40gb</option>
    <option value = 202>60gb</option>

</select>
<br><br>
CD-ROM
<input type = "checkbox" name = chkCDROM value="300" >
<br>
DVD-ROM
<input type = "checkbox" name = chkDVD value="301" >
<br>
Scanner
<input type = "checkbox" name = chkScanner value="302" >
<br>
Desktop Case
<input type = "radio" name = radCase value="400" >
<br>
Mini Tower
<input type = "radio" name = radCase value="401" >
<br>
Full Tower
<input type = "radio" name = radCase value="402" >
<br>
<P>
<input type = "button" value = "Update" name=butUpdate onclick="updateOrderDetails()">
</P>
</TD>
<TD>
<textarea rows = 20 cols = 35 id=txtOrder name=txtOrder>


 
Old April 16th, 2007, 04:18 PM
Registered User
 
Join Date: Apr 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default


.................

<textarea rows = 20 cols = 35 id=txtOrder name=txtOrder>
</textarea>
</TD>
</TR>
</table>
</form>
</body>
</html>





 
Old March 1st, 2010, 06:37 PM
Registered User
 
Join Date: Mar 2010
Posts: 8
Thanks: 3
Thanked 0 Times in 0 Posts
Default [document.form1.cboProcessor.selectedIndex]

Is anyone able to explain why it's necessary to use square brackets for this bit of code:

document.form1.cboProcessor[document.form1.cboProcessor.selectedIndex]

I don't understand why you can't just do formElement=document.form1.cboProcessor.selectedIn dex

Obviously if you do this it will display 'undefined'. But why is this the case?

Thanks!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Question on Chapter 5 arholly BOOK: Expert Access 2007 Programming ISBN 978-0-470-17402-9 1 September 2nd, 2008 04:07 PM
Chapter 4 question davidle1234 BOOK: ASP.NET 2.0 Website Programming Problem Design Solution ISBN: 978-0-7645-8464-0 2 January 18th, 2007 12:18 AM
Chapter 16 Question SomeDude BOOK: Beginning VB.NET 2nd Edition/Beginning VB.NET 2003 4 October 24th, 2005 06:13 PM
Another chapter 10 question. czambran BOOK: Beginning CSS: Cascading Style Sheets for Web Design ISBN: 978-0-7645-7642-3 4 April 3rd, 2005 09:12 PM





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