SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Germany
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Calculating price

    Hi there,

    I need to calculate a price based on selections made by the client. The following works fine, but I would need to add some extra options which I will explain below the code.

    Code:
    function calculatePrice() {
    	ammount = 0;
    	//Calculating prices
    	if (document.reg.fee[0].checked) {
    		ammount = parseInt(ammount + 995);
    	}
    	if (document.reg.fee[1].checked) {
    		ammount = parseInt(ammount + 1395);
    	}
    	if (document.reg.fee[2].checked) {
    		ammount = parseInt(ammount + 645);
    	}
    	if (document.reg.fee_accomp_person.checked) {
    		ammount = parseInt(ammount + 100);
    	}
    
    	// Total that will be displayed
    	document.reg.fee_total.value = parseInt(ammount);
    }
    Now I would need an extra drop down box which holds dynamic data. There are two possibilities. Either the value starts with REG or with DIS, so REG has a price and DIS has a price. Unfortunately the whole value will be something like REG0001, REG0002 and so on. The dropdown will mostlikely hold multiple REG or DIS values.

    Is there a why with Javascript to check only the first three letters to find out which price is applicable? (Wildcard)

    How whould I address a drop down box? something like document.reg.dropdown.value ?

    Thanks for any help,

    regards ASchweti

  2. #2
    SitePoint Guru Vincent Puglia's Avatar
    Join Date
    Feb 2000
    Location
    where the World once stood
    Posts
    700
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    quick answer: not really.
    what are you trying to do have the 'reg' and 'dis' separate out? before, during, or after the selection? If before, simply sort the array before creating the selection list. if during, the hitting 'r' should bring up the first 'reg'. if after, why?

    more info is needed.

    Vinny
    Where the World Once Stood
    the blades of grass
    cut me still

  3. #3
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Germany
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    I am using the onClick event, for a dropdown most likely onChange.

    So if I check the box named fee_accomp_person (please see code above) 100 gets added to the ammount.

    If the value of the dropdown start with "reg" I would like to add e.g. 200 to the ammount if it start with dis only 150.

    Is that what your were asking for?

    Regards ASchweti

  4. #4
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Germany
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am thinking of something like...

    Code:
    if (document.reg.dropdown.value == 'reg%') {ammount1 = parseInt(ammount1 + 150);}
    ..with the onChange handler which doesn't work

    Regards ASchweti

    PS: This code might be totally dumb, but I am NOT a Javascript programmer

  5. #5
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    var f = document.reg;
    var value = f.dropdown.options[f.dropdown.selectedIndex].value;
    var valueType = value.subtstring( 0, 3 );

    valueType now holds "REG" or "DIR" dependant upon what is selected. This may not be the best way to integrate it into your script, but I think you get the idea.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  6. #6
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Germany
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi beetle,

    I think I get the idea, but I get "Object doesn't support this method". Only thing I changed compared to your code is the "dropdwon", since the name of the dropdown box is actually "client".

    Any idea?

    Regards ASchweti

  7. #7
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hehe, that's because I typoed substring as "subtstring"

    Change it and you should be good.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  8. #8
    SitePoint Zealot
    Join Date
    Jun 2003
    Location
    Germany
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh Jesus, could have seen that as well. I even read about substring in my smart O'Reilly book, but didn't see the typo

    Well, works fine...

    Thanks for your support,

    ASchweti


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
  •