+ Reply to Thread
Results 1 to 5 of 5

Thread: Javascript Help - Total Beginner

  1. #1
    Conor's Avatar
    Conor is offline Lord Of The Keys Conor is an unknown quantity at this point
    Join Date
    Jan 2005
    Location
    Toronto
    Posts
    1,785

    Javascript Help - Total Beginner

    Hey guys,

    I'm a total beginner in Javascript and I need help! I have an assignment due tomorrow and I can't get this figured out! I'll try to break it down for you as best I can.

    The goal: When the 'Process' button is clicked, the inputted 'Price' multiplies by the number of items inputted and puts that value into the Sale Value field. It also adds the sale value to the Customer Total field value and increases the 'Items Purchased' field value by 1.

    Here's all of my code.

    Code:
    <html>
    <head>
    <title>Assignment 5</title>
    
    
    <script type="javascript">
    
    	function process(price, number)
    		{
    var value = document.form.price.value * document.form.number.value;
    
    		        document.form.salevalue.value = document.form.price.value * document.form.number.value;
    			document.form.customertotal.value = document.form.salevalue.value + value;
    			document.form.itemnumber.value = document.form.itemnumber.value + 1; 
    		}
    		
    
    </script>
    
    
    </head>
    <body>
    
    <p><b><u>East Residence Online Organic Store</u></b></p>
    
    <br>
    
    <table border="1" width="400">
    <form name="form">
    <tr>
    	<td><b>Customer Number</b>
    		<input type="text" name="customernumber" value="1" />
    	</td>
    	<td><b>Home delivery</b>
    		<input type="checkbox" name="homedelivery" value="Home" checked />
    	</td>
    </tr>
    
    <tr>
    	<td><b>Items purchased</b></td>
    	<td>
    		<input type="text" name="itemspurchased" />
    	</td>
    </tr>
    
    <tr>
    	<td><b>Item number</b></td>
    	<td>
    		<input type="text" name="itemnumber" value="1" />
    	</td>
    </tr>
    
    <tr>
    	<td><b>Price</b></td>
    	<td>
    		<input type="text" name="price" />
    	</td>
    </tr>
    
    <tr>
    	<td><b>Number</b></td>
    	<td>
    		<input type="text" name="number" />
    	</td>
    </tr>
    
    <tr>
    	<td><b>Value of sale</b></td>
    	<td>
    		<input type="text" name="salevalue" value="0" />
    	</td>
    </tr>
    
    <tr>
    	<td><b>Customer Total</b></td>
    	<td>
    		<input type="text" name="customertotal" value="0" />
    	</td>
    </tr>
    
    <tr>
    	<td>&nbsp;</td>
    	<td>
    		<input type="button" name="Process" value="Process" onclick=" process()" />
    	</td>
    </tr>
    </form>
    </table>
    
    </body>
    </html>
    My HTML should be more or less right on, but I have no idea with the Javascript. Please help!

  2. #2
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Javascript Help - Total Beginner

    Very important: don't dump all your code and say "it doesn't work". You did a good job describing what the code is supposed to do, but neglected to say what it does (including error messages). I'm not sure if it was intentional, but you posted a rather nice minimal test case. Keep doing that in the future.

    The single best bit of advice I can give: use Firefox (if not already) and Firebug to debug your pages. You can use it to work with JS, CSS and even HTML.

    Make sure you follow the advice in the sig and read all the links, though you don't have to do this before turning in the assignment.

    The source is rather simple, so let's continue

    Quote Originally Posted by Conor View Post
    HTML Code:
    <script type="javascript">
    The "type" attribute should be a MIME type (also called a "content type" in the HTML standard), such as "text/javascript".

    Quote Originally Posted by Conor View Post
    Code:
        function process(price, number)
        {
            var value = document.form.price.value * document.form.number.value;
    "document.form" may be undefined on some browsers. If you want to refer to a form by name, use "document.forms.name" or the equivalent "document.forms[name]"; the former is for a literal form name, the latter for when the form name is stored in a variable. In this case, you can use "document.forms.form". Even better, pass the form to "process()" when it's called:

    HTML Code:
        function process(form) {
        ....
    <input type="button" name="Process" value="Process" onclick="process(this.form)" />
    In an inline event handler, "this" refers to the target; in this case, "this" is the button named "Process". Furthermore, every form input has a "form" property referring to the containing form.

    Quote Originally Posted by Conor View Post
    Code:
            var value = document.form.price.value * document.form.number.value;
    
            document.form.salevalue.value = document.form.price.value * document.form.number.value;
            document.form.customertotal.value = document.form.salevalue.value + value;
    Look closely at these three lines. They can be shortened, reducing unnecessary calculation and fixing the bug described next.

    Quote Originally Posted by Conor View Post
    Code:
            document.form.customertotal.value = document.form.salevalue.value + value;
            document.form.itemnumber.value = document.form.itemnumber.value + 1;
    Since one of the arguments is a string in both these lines, the "+" is a concatenation operator, not addition. Use parseInt or unary "+". Read "Javascript Type-Conversion" for more information.

    Quote Originally Posted by Conor View Post
    HTML Code:
    <table border="1" width="400">
    <form name="form">
    The only valid children for a <table> are <caption>, <col>, <colgroup>, <thead>, <tfoot> and <tbody> (basically in that order) and (for backwards compatibility), except that <tbody> may be omitted if there's only one table body and no head or foot section. In the last case, <tr> may be a child of <table>. No other element, including <form>, can be a child of <table>. You can use W3C's validator to check for violations such as this.

    The name "form" is a very poor name as you should always choose descriptive names, but it's a small concern for a homework problem.

    You shouldn't use table based layouts, but (again) as this is a homework assignment, creating a CSS based layout is a low priority. Still, if you've the time, don't get into bad habits. "Applying CSS to forms" and "Fancy Form Design Using CSS" will help you with CSS form layout. Read "Designing Web Forms" and the pages it links to for info on designing forms.

    Protip: use [HTML] and [PHP] rather than [CODE] when appropriate for pretty colors (see above for examples).
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  3. #3
    apoorav's Avatar
    apoorav is offline x10 Sophmore apoorav is an unknown quantity at this point
    Join Date
    Oct 2008
    Location
    INDIA
    Posts
    110

    Re: Javascript Help - Total Beginner

    You should refer to tutorials at W3schools
    If you feel my post is usefull then click on to give me Reputation

  4. #4
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Javascript Help - Total Beginner

    Quote Originally Posted by apoorav View Post
    You should refer to tutorials at W3schools
    W3Schools is a mixed bag. Some good, some terrible. Besides, OP is already in a class. Not that other sources couldn't help, depending on the quality of his current instruction, but that's probably requires more time than OP has free.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

  5. #5
    leonard9 is offline x10Hosting Member leonard9 is an unknown quantity at this point
    Join Date
    Oct 2009
    Posts
    5

    Re: Javascript Help - Total Beginner

    You can also google java codeing and you will find a page on it quicker than posting

+ Reply to Thread

Similar Threads

  1. drop down menus with JavaScript disabled?
    By sifaka in forum Free Hosting
    Replies: 1
    Last Post: 05-15-2008, 10:46 AM
  2. javascript and external javascript files problem
    By delon in forum Programming Help
    Replies: 6
    Last Post: 04-27-2008, 12:41 AM
  3. XML and Javascript
    By cuteboytm in forum Graphics & Webdesign
    Replies: 1
    Last Post: 09-21-2007, 10:00 AM
  4. Some nice-simple JavaScript effects !!
    By careerbridge in forum Scripts & 3rd Party Apps
    Replies: 1
    Last Post: 07-13-2006, 08:36 AM
  5. Rome : Total War Barbarian Invasion
    By Abusi in forum Gamer's Lounge
    Replies: 0
    Last Post: 03-06-2006, 10:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers