Newbie help with javascript code?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 9 of 9

Thread: Newbie help with javascript code?

  1. #1
    Join Date
    Jul 2005
    Posts
    4

    Newbie help with javascript code?

    I'm just now starting to mess around with javascript, and I wrote this code as a calculator to figure what price bracket customers would be in for color copies. It would return a "price per copy" in a field when the customer entered their quantity. I can't get the code to do anything though. I might be way off... Thanx in advance for the help?

    <HTML>
    <HEAD>
    <TITLE>Color Copies Calculator</TITLE>
    <SCRIPT LANGUAGE="Javascript"><!-- Hide from old browsers
    function figure(){
    var quantity = document.form.count.value();
    var return = document.form.pricepercopy.write();
    if (quantity <= 50) return .69;
    if (quantity <= 100) return .42;
    if (quantity <= 150) return .37;
    if (quantity <= 300) return .33;
    if (quantity <= 500) return .29;
    }
    // Stop hiding from old browsers -->
    </SCRIPT>

    </HEAD>
    <BODY>
    <P><FORM NAME="form">
    <INPUT TYPE="TEXT" NAME="count" SIZE="24"><BR>
    <INPUT TYPE="TEXT" NAME="pricepercopy" SIZE="24"><INPUT TYPE="SUBMIT" VALUE="Get My Price" onClick="figure()"></P></FORM>
    </BODY>
    </HTML>

  2. #2
    Join Date
    Jun 2004
    Posts
    199
    First, this is a java forum. Not Javascript.

    try removing the "<!-- Hide from old browsers" and " -->"
    Also put an "alert('here');" at the top of the function.

    Is there a JS error showing up (look at the bottom left of the browser).

  3. #3
    Join Date
    Jul 2005
    Posts
    4
    oops....do I move it?

    do I put the alert above the function or right above the variable?
    and there is no error showing up...

  4. #4
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Will the logic of this work? If you have a count less than 100, isn't that count less than the higher quatity cut-off's, too? So which value is the one you want?

    If this is where your script is not working, determine whether the count is larger than a lower value AND less than the upper level then you know you are in a specific "range" ...

  5. #5
    Join Date
    Jun 2004
    Posts
    199
    above the variable

    alert('here');
    var quantity = document.form.count.value();

  6. #6
    Join Date
    Jun 2004
    Posts
    199
    Now that I look at it more.

    Don't use return as a var.

    And your function looks like it returns the number but does nothing with it.

  7. #7
    Join Date
    Jul 2005
    Posts
    4
    so with those changes, I'm still not getting anything?
    what does the (alert) do?

    <HTML>
    <HEAD>
    <TITLE>Color Copies Calculator</TITLE>
    <SCRIPT LANGUAGE="Javascript">
    function figure(){
    alert('here');
    var quantity = document.form.count.value();
    var return = document.form.pricepercopy.write();
    if (quantity <= 50 && >= 0) return .69;
    if (quantity <= 100 && >= 50) return .42;
    if (quantity <= 150 && >= 100) return .37;
    if (quantity <= 300 && >= 150) return .33;
    if (quantity <= 500 && >= 300) return .29;
    }
    </SCRIPT>

    </HEAD>
    <BODY>
    <P><FORM NAME="form">
    <INPUT TYPE="TEXT" NAME="count" SIZE="24"><BR>
    <INPUT TYPE="TEXT" NAME="pricepercopy" SIZE="24"><INPUT TYPE="SUBMIT" VALUE="Get My Price" onClick="figure()"></P></FORM>
    </BODY>
    </HTML>

  8. #8
    Join Date
    Jul 2005
    Posts
    4
    so i was using "return" as a variable to write to the "pricepercopy" field.... how would I make it right that value to the "pricepercopy" field?

  9. #9
    Join Date
    Jun 2004
    Posts
    199
    alert will pop up a message box. since it is at the top and it never appears - that means your function is not being called OR it can't be called because the javascript is invalid or the calling of it is invalid.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center