SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    Ashburn, VA
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    JS picking wrong type

    Can anyone tell me if there is something wrong with my code? I have dropdown menu that lists payment methods. If the user selects one of the credit cards on the list, I want the JS to calculate the fee and show in another form field on screen.
    For some reason, no matter what is selected it keeps using the default value for the calculation. HELP!!

    Here is the js code that fires when a Calculate button is clicked.
    Code:
    function checkCC(){
    	var type  = document.getElementById("paymentmethod");
    	switch (type)
    	{
    	case 'Visa':
    	var perct = .03;
    	break
    	case 'American Express':
    	var perct = .0325;
    	break
    	case 'Master Card':
    	var perct = .003;
    	break
    	default:
    	var perct = 5;
    	  }
      	cogs = document.mainform.cogs.value
    	document.mainform.creditfee.value = (cogs*perct).toFixed(2);
      }

  2. #2
    SitePoint Enthusiast
    Join Date
    Nov 2006
    Posts
    99
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    One solution may be is to check selected value:
    Code JavaScript:
    var type  = document.getElementById("paymentmethod").selectedIndex;
    In that case you should have an value in option tag:
    Code HTML4Strict:
    <option value='0'>Visa</option>
    <option value='1'>American Express</option>
    and so on...
    And then check in switch:
    Code JavaScript:
    switch (type)
    	{
    	case 0: //it's Visa
    	var perct = .03;
    	break

  3. #3
    SitePoint Guru
    Join Date
    Apr 2006
    Posts
    802
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your type variable evaluates to an object-presumably the select element, and you are testing for a string- the switch will never match your cases.

  4. #4
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    Ashburn, VA
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks fellas.


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
  •