snippet library logo
in category
Javascript / Forms / Currency Selector
Snippet details
ID: 492
Viewed: 1320
Added: 2002-09-25
Version: 1
View Demo

User Rated at:
Rate This:
Snippets in this catagory         
Show Printable Version
This little sciprt makes it so you can have the user select which currency is appropriate for their country.

very easy to setup and run.

Browsers: IE5+

General Details
Snippet uploaded by: snippet
Email : webmaster@snippetlibrary.com
Snippet By: kdjoergensen

<!---Head--->
// can go in between the body tags also

<script language="javascript">
<!--

// the set function will populate the select element when called
function set(curr,form){

// two arrays are created: one for USD and one for EUR. The first
//line is for text (what is visible to the user0 the 2nd line is the
//value component */

var rangeUSD = {
    txt :  ["choose US Computer","<$2000","$2001-$5000",">$5000"],
    val :  ["#", "lowD","mediumD","highD"]
};
               
var rangeEUR = {
    txt :  ["choose Eur Computer", "<e1800","e1801-e4000",">e4000","e2002"],
    val :  ["#", "lowE","mediumE","highE","specialE"]
};


// get ready to make our selection
var useCurrency = new Array();

// if the selected radio element (captured in the variable "curr")
// has a value of "usd" then we set our useCurrency array equal to 
// the rangeUSD array defined above. Otherwise use the rangeEur array.

if (curr.value == "usd"){ // usd chosen
 useCurrency = rangeUSD;
} else if (curr.value == "eur"){ // eur chosen
 useCurrency = rangeEUR;
} else {
  alert('Something went wrong');
  return false;
}

// how many options exists for our chosen currency (may vary)
var numberOfItems = useCurrency['txt'].length;

// lets create an object reference to the select element 
// which we have named pricerange (see the html part)

var selObj = form.pricerange;

// deletes existing options which may show in the select element
for (var i=0;i< selObj.options.length; i++){
   selObj.options[i].text = "";
   selObj.options[i].value = "";
}

// we loop through each number of options to be added to the select
// element and populate it with the corresponding data in our chosen
// array 

for (var i=0;i< numberOfItems ;i++){
  // if option does not exist alredy we create one
  selObj.options[i] = new Option(); 
  selObj.options[i].text = useCurrency['txt'][i];
  selObj.options[i].value = useCurrency['val'][i];
}

// we set the cheapest model as our pre-selected option
selObj.options[0].selected = true;

}

// this function simply navigates to another page when
// user choose a specific price range. You can do anything
// you want instead (this is just an example)

function navigate(selElm){

// get value of the selected option
var sel = selElm.options[selElm.selectedIndex].value;

 //do not proceed if value is still '#" (our saftey valve)
 if (sel == "#") {
    return false; 
 } else {
   window.location.href = sel+".html";
 }

}

//-->
</script>

<!---Body--->


<form name="money">
Select Currency:<br />
<!-- if the user selects 'usd' then we call set with a reference to 'this' element -->
USD <input type="radio" name="currency" value="usd" onclick="set(this, this.form)"><br />
<!-- if the user selects 'eur' then we call set with a reference to 'this' element -->
EUR <input type="radio" name="currency" value="eur" onclick="set(this, this.form)"><br />
<br />
<!-- here our select element start. we have given it a name 'pricerange' -->
Select Price Range:<br />
<!-- the onselect eventhandler will call the navigate function when the user selects an option -->
<select name="pricerange" onchange="navigate(this)">
<option value="#">Please select currency first</option>
</select>
<br />
</form>
<br />




No Reviews to show


Please completely fill out the form below if you want to review this snippet. All reviews are subject to validation.


Subject:

Reviewed By:

Write a review:





Terms of Conditions
Powered By
Avian Hosting
© 2005 snippetlibrary.com All Rights Reserved.