Off-topic Talk Where overpaid, underworked S2000 owners waste the worst part of their days before the drive home. This forum is for general chit chat and discussions not covered by the other off-topic forums.

any javascript experts here? i need help!

Thread Tools
 
Old Nov 10, 2003 | 07:13 AM
  #1  
mav's Avatar
mav
Thread Starter
Registered User
 
Joined: May 2001
Posts: 7,337
Likes: 3
From: Los Angeles, Miami
Default any javascript experts here? i need help!

I'm trying to integrate form checkbox functionality into this script that was built for drop down menus. I passing an onClick event with the price as a value.

PHP Code:
onClick="showPrice($100);" 
Can someone help? Thanks.

PHP Code:
  function showPrice(form)
  {  
    var myTotalPrice = 0;
    var showUP = 0;
    var myMathProblem = "";
    myItemPrice = parseFloat(form.nuPrice.value);
    for (var i = 0; i < form.elements.length; i++)
    {
      var e = form.elements[i];
      if ( e.type == 'select-one' )
      {
        showUP = 1;
        Item = e.selectedIndex;
        myPrice = e.options[Item].text;
        myDollarSign = myPrice.indexOf("$",0)
        if ( myDollarSign != "-1" )
        {
          myParSign = myPrice.indexOf(")", myDollarSign);
          myAttributeString = myPrice.substring(myDollarSign+1, myParSign);
          myAttributeString = myAttributeString.replace(/,/,"");
          myAttributePrice = parseFloat(myAttributeString);
          myMathProblem = myPrice.charAt(myDollarSign - 1);
        } else { myAttributePrice = 0; }
          if (myMathProblem == "-")
          {
            myTotalPrice = myTotalPrice - myAttributePrice;
          } else {
            myTotalPrice = myTotalPrice + myAttributePrice;
          }
      }
    }  
    if ( showUP )
    {
        myTotalPrice = FormatNumber(myTotalPrice + myItemPrice);
        document.getElementById("productNEWprice").innerHTML = "Subtotal Price with Options $" + myTotalPrice;
    }
  } 
Reply
Old Nov 10, 2003 | 09:13 AM
  #2  
AgS2K's Avatar
Registered User
 
Joined: May 2001
Posts: 900
Likes: 0
From: Richmond, Virginia
Default

I'm assuming since you're using checkboxes, that there will be more than one selectable item on the screen (more than one checkbox) and that the displayed price would need to be updated when a user checks or unchecks the checkbox, yes?

There are several ways to do this, but if there aren't going to be hundreds of checkboxes on the screen, probably the easiest way to do this is to have the checkbox code look like this:

PHP Code:
<input type="checkbox" name="fieldName" value="100" onClick="showPrice()">" 
Then change the script to this (assumes that there is only one form on the page):

PHP Code:
function showPrice()
{
    var totalPrice = 0;
    var form = document.forms[0];
    for(i=0;i<form.elements.length;i++)
    {
        if(form.elements[i].type == checkbox && form.elements[i].checked == true)
        {
            totalPrice += parseFloat(form.elements[i].value);
        }
    }
    document.getElementById("productNEWprice").innerHTML = "Subtotal Price with Options $" + totalPrice;
} 
The problem with the original script is that it's trying to do a bit more than this. For instance, it utilizes a FormatNumber() function you don't list here.

Hope this helps!
Reply
Old Nov 10, 2003 | 09:25 AM
  #3  
mav's Avatar
mav
Thread Starter
Registered User
 
Joined: May 2001
Posts: 7,337
Likes: 3
From: Los Angeles, Miami
Default

Thanks AgS2K... Basically this is part of a complex BTO Computer Configurator script. I'm trying to use it to automatically update the display price as the user selects different options.

My main problem is getting this to work with the drop down menus as well as the checkboxes.

The FormatNumber() function is this:

PHP Code:
   function FormatNumber(num)
   {
     if(isNaN(num)) { num = "0"; }
     sign = (num == (num = Math.abs(num)));
     num = Math.floor(num*100+0.50000000001);
     cents = num%100;
     num = Math.floor(num/100).toString();
     if(cents<10) { cents = "0" + cents; }
     for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
     {
       num = num.substring(0,num.length-(4*i+3))+','+ num.substring(num.length-(4*i+3));
     }
     return (((sign)?'':'-') + num + '.' + cents);
   } 
Thanks for your help, I'll see if I can integrate it together.
Reply
Old Nov 10, 2003 | 11:36 AM
  #4  
AgS2K's Avatar
Registered User
 
Joined: May 2001
Posts: 900
Likes: 0
From: Richmond, Virginia
Default

Then I would suggest having the "totalPrice" variable as a global variable (initially set outside of any function). If you can't integrate the two showPrice() scripts, have two of them, named differently, of course, one for the drop downs, one for the checkboxes, that each update the "totalPrice" variable and write the value onto the screen.

But there are at least half-a-dozen other ways to do this...
Reply
Related Topics
Thread
Thread Starter
Forum
Replies
Last Post
AP1Sean
Off-topic Talk
0
Jun 29, 2004 01:45 PM
alexf20c
The Corner
2
Mar 24, 2004 08:35 PM
DC5 Kid
Off-topic Talk
0
Jun 13, 2002 07:03 AM
huyto
Off-topic Talk
0
May 10, 2001 02:24 PM




All times are GMT -8. The time now is 11:47 AM.