SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast ElQanah's Avatar
    Join Date
    Oct 2004
    Location
    Costa Rica
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question how to handle exceptions within options of a select

    Here is the game: I have two selects, automatically filled with PHP from a database, however there is an exception in a combination of values, let's say:

    Select1:
    Code:
    <option value="0.22" id="Argentina">Argentina</option>
    <option value="0.33" id="Brazil">Brazil</option>
    Select2:
    Code:
     <option value="0.11" id="USA">USA</option>
     <option value="0.44" id="UK">UK</option>
    So, with a little JS I take the submited values and get a total. All works great, but we have one single case where a combination must be different.
    For example, if Argentina and UK are selected, then 0.22 + 0.44 is 0.66
    Well, in that particular case, the value need to be altered to total 0.77

    I thougth of making an IF clause within the little JS calculator function I have, but not sure what's the best way to do this, any ideas or suggestions?

    I am not sure if I can take the ID of the <option> in both sides and perform the IF statement based on the ID (can you actually grab it somehow?)

  2. #2
    SitePoint Enthusiast ElQanah's Avatar
    Join Date
    Oct 2004
    Location
    Costa Rica
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Workaround using index

    For someone who is trying something similar (probably nobody!) here is my workaround:

    I used the index of the selected item to validate the selection in both selects:
    Code:
     var i1= document.myform.country1.selectedIndex;
     var i2= document.myform.country2.selectedIndex;
     var fa = parseFloat(document.myform.country1.options[i1].value);
    var fb = parseFloat(document.myform.country2.options[i2].value);
    var f1;
    f1 = (fa + fb);
    	   if (i1 == 28 && i2 == 95){f1=0.10;}
    document.myform.txtRate.value = fnb(Math.round(f1*1000)/1000,2);


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
  •