Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder
    Join Date
    Feb 2007
    Posts
    219
    Thanks
    25
    Thanked 1 Time in 1 Post

    currency converter

    I've seen a cool currency conversion script at: http://www.cssnewbie.com/example/currency-conversion/

    Now I want to implement this script within my own webpage, by getting rid of the convert link and the Jquery part. I want the script to fire when the input box loses focus.

    What I've got so far:
    Code:
    <script type="text/javascript">
    function toUSD(number) {
        var number = number.toString(), 
        dollars = number.split('.')[0], 
        cents = (number.split('.')[1] || '') +'00';
        dollars = dollars.split('').reverse().join('')
            .replace(/(\d{3}(?!$))/g, '$1,')
            .split('').reverse().join('');
        return '$' + dollars + '.' + cents.slice(0, 2);
    }
    </script>
    
    <input type="text" name="ppr" onchange="javascript:toUSD(#ppr);">

    But it ain't working. I'm a real javascript noob, so I hope someone can help me out on this one.

  • #2
    Senior Coder
    Join Date
    Mar 2005
    Location
    Portsmouth UK
    Posts
    4,529
    Thanks
    3
    Thanked 511 Times in 498 Posts
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    </head>
    
    <body>
    <script type="text/javascript">
    function toUSD(number) {
        var number = number.toString(),
        dollars = number.split('.')[0],
        cents = (number.split('.')[1] || '') +'00';
        dollars = dollars.split('').reverse().join('')
            .replace(/(\d{3}(?!$))/g, '$1,')
            .split('').reverse().join('');
        return '$' + dollars + '.' + cents.slice(0, 2);
    }
    </script>
    
    <input type="text" name="ppr" onblur="this.value=toUSD(this.value);">
    </body>
    
    </html>
    better

    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    </head>
    
    <body>
    <script type="text/javascript">
    function toUSD(obj) {
     var number=obj.value.split('.'),
     dollars=number[0].replace(/\D/g,'');
     cents=((number[1] || '')+'00').replace(/\D/g,'');
     obj.value='$'+(dollars||0)+'.'+cents.slice(0,2);
    }
    </script>
    
    <input type="text" name="ppr" onblur="toUSD(this);">
    </body>
    
    </html>
    Last edited by vwphillips; 05-19-2013 at 02:45 PM. Reason: better code
    Vic

    God Loves You and will never love you less.

    http://www.vicsjavascripts.org/Home.htm

    If my post has been useful please donate to http://www.operationsmile.org.uk/

  • Users who have thanked vwphillips for this post:

    docock (05-19-2013)


  •  

    Posting Permissions

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