SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Mar 2002
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Exclamation Javascript Price Check Script

    Friends,

    I have created a little script in order to add VAT to prices, check decimals, and add point/comma for thousands.

    The result should be then displayed to a <SPAN> tag in the HTML.

    Unfortunately the system is not working ... and I really wonder what's wrong ...


    Here's the full script with HTML part. Thanks for any advice you could give ...

    =D

    powergen

    [VBS]
    function js_VAT_<?php echo $rec["id"]; ?> (price) {

    var price_VAT = (price*1,20);

    if (price_VAT.lastIndexOf(",")!=-1) {
    var division = price_VAT.split(",");

    if (division[1].length==1) {
    price_VAT = price_VAT + "0";
    }

    if (price.lastIndexOf(",")==-1) {
    price_VAT = price_VAT + ",00";
    }

    if (division[0]>3 && price.indexOf(".")==-1) {

    var number="";
    var distance=division[0].length - 3;

    for (var i=0; i<distance; i++) {
    number = number + charAt(i);
    }

    price = number + "." + substring((price.length-3), (price.length-1));

    }
    }

    document.getElementById('price_VATTED_<?php echo $rec["id"]; ?>').innerHTML=price_VAT;

    }

    [/VBS]


    The result should be called by

    <span id="price_VATTED_<?php echo $rec["id"]; ?>"></span>


    Thanks again, power

  2. #2
    SitePoint Member
    Join Date
    Jan 2003
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    First you must create the SPAN element, only then you can use the getElementById method.

  3. #3
    SitePoint Zealot
    Join Date
    Mar 2002
    Posts
    116
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks.

    I didn't know this.

    I have tried but still the function doesn't work.



    I have found and error and corrected it ... here's the function as it appears now.

    What is unknown to me is whether JavaScript understands automatically how to deal with strings and numbers.

    I say this since I add to numbers commas and full stops ... I don't know whether this is possible or JS rejects this since those are initially just numbers.

    Any clue on this ?

    Thanks again, new version follows ...


    [VBS]
    function js_VAT_<?php echo $rec["id"]; ?> (price) {

    var price_VAT = (price*1,20);

    if (price_VAT.lastIndexOf(",")!=-1) {
    var division = price_VAT.split(",");

    if (division[1].length==1) {
    price_VAT = price_VAT + "0";
    }

    if (price.lastIndexOf(",")==-1) {
    price_VAT = price_VAT + ",00";
    }

    if (division[0]].length>3 && price.indexOf(".")==-1) {

    var number="";
    var distance=division[0].length - 3;

    for (var i=0; i<distance; i++) {
    number = number + charAt(i);
    }

    price = number + "." + substring((price.length-3), (price.length-1));

    }
    }

    document.getElementById('price_VATTED_<?php echo $rec["id"]; ?>').innerHTML=price_VAT;

    }

    [/VBS]
    Last edited by powergen; Jan 14, 2003 at 08:40.


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
  •