SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Addict
    Join Date
    Oct 2003
    Location
    Tenerife, Spain / UK
    Posts
    329
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JavaScript Multiplying form values

    Hi

    I have a very simple HTML form
    Code:
    <form id="form1" name="form1 action="" method="">
    <input type="text" id="unitcost" name="unitcost" />
    <input type="text" id="quantity" name="quantity" />
    <input type="text" id="sub" name="sub" disabled="disabled" />
    </form>
    I want the values of the fields "unitcost" and quantity to be multipled together and shown in the disabled field "sub".

    I'm using the onchange() method (in the unitcost and quantity fields) to call the following JavaScript Function which is clearly wrong.

    <script type="text/javascript" >
    function updatesub(){
    var unitcost = document.getElementById(unitcost);
    var quantity = document.getElementById(quantity);
    u = unitcost.value;
    q = quantity.value;
    subtotal = u*q;
    document.getElementById("sub").setAttribute("value",subtotal)
    }
    </script>

    What's wrong with my function? (Very new to JavaScript)
    David Parkes
    Nuclear Internet - Windows Web Hosting
    http://www.nuclearinternet.com

  2. #2
    SitePoint Enthusiast David.A's Avatar
    Join Date
    May 2010
    Location
    Milky Way Galaxy
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    Code:
    function updatesub(){
    var u = parseInt(document.getElementById(unitcost).value, 10);
    var q = parseInt(document.getElementById(quantity).value, 10);
    subtotal = u*q;
    
    document.getElementById("sub").value=subtotal;
    }
    FYI, your original u and q were completely wrong. They were missing the var statement.

  3. #3
    SitePoint Addict
    Join Date
    Oct 2003
    Location
    Tenerife, Spain / UK
    Posts
    329
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by David.A View Post
    Try this:

    Code:
    function updatesub(){
    var u = parseInt(document.getElementById(unitcost).value, 10);
    var q = parseInt(document.getElementById(quantity).value, 10);
    subtotal = u*q;
    
    document.getElementById("sub").value=subtotal;
    }
    FYI, your original u and q were completely wrong. They were missing the var statement.
    Thanks for your help, but that doesn't seem to work either. Looking for a solution that works cross-browser and this didn't seem to work in Firefox.
    David Parkes
    Nuclear Internet - Windows Web Hosting
    http://www.nuclearinternet.com

  4. #4
    SitePoint Member
    Join Date
    May 2010
    Posts
    17
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You guys are silly. You forgot the "var" before "subtotal"

  5. #5
    SitePoint Addict
    Join Date
    Oct 2003
    Location
    Tenerife, Spain / UK
    Posts
    329
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah but ....

    Code:
    <script type="text/javascript" >
    function updatesub(){
    var u = parseInt(document.getElementById(unitcost).value);
    var q = parseInt(document.getElementById(quantity).value);
    var subtotal = u*q;
    	document.getElementById("sub").value=subtotal;
    
    }
    </script>

    Doesn't work either.
    David Parkes
    Nuclear Internet - Windows Web Hosting
    http://www.nuclearinternet.com

  6. #6
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,810
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    You left off the quotes around unitcost and quantity in the getElementById statements.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">

  7. #7
    SitePoint Addict
    Join Date
    Oct 2003
    Location
    Tenerife, Spain / UK
    Posts
    329
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks syntax is a *******
    David Parkes
    Nuclear Internet - Windows Web Hosting
    http://www.nuclearinternet.com

  8. #8
    SitePoint Enthusiast David.A's Avatar
    Join Date
    May 2010
    Location
    Milky Way Galaxy
    Posts
    66
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try using a lint with your JS if it doesn't work.

    http://jslint.com/
    http://javascriptlint.com/

  9. #9
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,702
    Mentioned
    101 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by David.A View Post
    Try using a lint with your JS if it doesn't work.
    As a warning to others though, jslint.com advertises that it will make you cry.
    It's a harsh mistress, but with good reason.

    As an example, when "The Good Parts" is clicked, and you select "assume a web browser", here is what jslint.com has to say about the following script from earlier:

    Code javascript:
    function updatesub(){
    var u = parseInt(document.getElementById(unitcost).value);
    var q = parseInt(document.getElementById(quantity).value);
    var subtotal = u*q;
    	document.getElementById("sub").value=subtotal;
     
    }

    is reported to have 15 problems.

    1. "use strict"; allows Strict Mode for your code, which provides extra protections. Here's some info from John Resig about strict mode.

    2. Placing a space between the parenthesis and the curly brace is one of a standard set of code conventions that aid and enhance readability of the code.

    3. Indent the function code by four spaces, a code convention for readability. I adjusted the tab to be 4 spaces as well.

    4. unitcost is not defined, it should be text instead, as is quantity

    5. Missing radix parameter, which for parseInt means to specify a second parameter of 10, which means to default to decimal values.

    6. Too many var statements. It used to be a convention to use multiple var statements at the top of the code, but it's not recommended to use a sigle var statement.

    7. space-separate operators such as * and =

    which results in the following code that jslint.com is now completely happy with.

    Code javascript:
    "use strict";
    function updatesub() {
        var u = parseInt(document.getElementById('unitcost').value, 10),
            q = parseInt(document.getElementById('quantity').value, 10),
            subtotal = u * q;
        document.getElementById("sub").value = subtotal;
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  10. #10
    Programming Since 1978 silver trophybronze trophy felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, NSW, Australia
    Posts
    16,810
    Mentioned
    25 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by pmw57 View Post
    which results in the following code that jslint.com is now completely happy with.

    Code javascript:
    "use strict";
    function updatesub() {
        var u = parseInt(document.getElementById('unitcost').value, 10),
            q = parseInt(document.getElementById('quantity').value, 10),
            subtotal = u * q;
        document.getElementById("sub").value = subtotal;
    }
    which isn't quite what jslint expected though. What it is expecting you to do is

    Code javascript:
    function updatesub() {
        var u, q, subtotal;
        u = parseInt(document.getElementById('unitcost').value, 10);
        q = parseInt(document.getElementById('quantity').value, 10),
        subtotal = u * q;
        document.getElementById("sub").value = subtotal;
    }

    placing commas between the statements will break the code if you forget why they are there and either delete them or replace them with semicolons. The idea iis to define all of the local variables at the top regardless of where in the script you actually use it since JavaScript is going to work on the assumption that your code does this anyway.
    Stephen J Chapman

    javascriptexample.net, Book Reviews, follow me on Twitter
    HTML Help, CSS Help, JavaScript Help, PHP/mySQL Help, blog
    <input name="html5" type="text" required pattern="^$">


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •